From 99d93093c7eb2dff9d0ec45400bcdab36e0e86fc Mon Sep 17 00:00:00 2001 From: Victor Gerbrands Date: Thu, 4 May 2023 11:43:27 +0200 Subject: [PATCH] fix: wrap router.replace in useEffect hook --- components/product/variant-selector.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/product/variant-selector.tsx b/components/product/variant-selector.tsx index a555aeb7d..cb5d825cb 100644 --- a/components/product/variant-selector.tsx +++ b/components/product/variant-selector.tsx @@ -5,6 +5,7 @@ import { ProductOption, ProductVariant } from 'lib/medusa/types'; import { createUrl } from 'lib/utils'; import Link from 'next/link'; import { usePathname, useRouter, useSearchParams } from 'next/navigation'; +import { useEffect } from 'react'; type ParamsMap = { [key: string]: string; // ie. { color: 'Red', size: 'Large', ... } @@ -79,9 +80,11 @@ export function VariantSelector({ const currentUrl = createUrl(pathname, currentParams); const selectedVariantUrl = createUrl(pathname, selectedVariantParams); - if (currentUrl !== selectedVariantUrl) { - router.replace(selectedVariantUrl); - } + useEffect(() => { + if (currentUrl !== selectedVariantUrl) { + router.replace(selectedVariantUrl); + } + }, [currentUrl, router, selectedVariantUrl]); return options.map((option) => (