mirror of
https://github.com/vercel/commerce.git
synced 2025-05-16 06:26:58 +00:00
fix: wrap router.replace in useEffect hook
This commit is contained in:
parent
a46f39bd4c
commit
99d93093c7
@ -5,6 +5,7 @@ import { ProductOption, ProductVariant } from 'lib/medusa/types';
|
|||||||
import { createUrl } from 'lib/utils';
|
import { createUrl } from 'lib/utils';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
type ParamsMap = {
|
type ParamsMap = {
|
||||||
[key: string]: string; // ie. { color: 'Red', size: 'Large', ... }
|
[key: string]: string; // ie. { color: 'Red', size: 'Large', ... }
|
||||||
@ -79,9 +80,11 @@ export function VariantSelector({
|
|||||||
const currentUrl = createUrl(pathname, currentParams);
|
const currentUrl = createUrl(pathname, currentParams);
|
||||||
const selectedVariantUrl = createUrl(pathname, selectedVariantParams);
|
const selectedVariantUrl = createUrl(pathname, selectedVariantParams);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
if (currentUrl !== selectedVariantUrl) {
|
if (currentUrl !== selectedVariantUrl) {
|
||||||
router.replace(selectedVariantUrl);
|
router.replace(selectedVariantUrl);
|
||||||
}
|
}
|
||||||
|
}, [currentUrl, router, selectedVariantUrl]);
|
||||||
|
|
||||||
return options.map((option) => (
|
return options.map((option) => (
|
||||||
<dl className="mb-8" key={option.id}>
|
<dl className="mb-8" key={option.id}>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user