Next.js Discord

Discord Forum

searchParams update fetch data

Unanswered
Britannia Petite posted this in #help-forum
Open in Discord
Britannia PetiteOP
Hi! im using next.js 14

Im having a search feature, im using searchParams to get the current cue passed in url and Im uptading it with router.push. When I click on an item it should switch to my /item page (im using route interceptor here with a slot).
The page switch is OK but the url is instantly re-switching to /search (but I have my slot displayed) and I cant history back...

here is the hook im using :

export const useQueryParamsUpdate = () => {
    const searchParams = useSearchParams();
    const newParams = useMemo(
        () => new URLSearchParams(searchParams.toString()),
        [searchParams]
    );
    const router = useRouter();

    const queryParamsUpdate = useCallback(
        (cue: string) => {
            newParams.set('cue', cue);
            router.push(`/search?${newParams.toString()}`);
        },
        [newParams, router]
    );

    return { queryParamsUpdate };
};


queryParamsUpdate is retriggering due to searchParams changed on route changed and its pushing /search route ... don't know how to deal with that

1 Reply

Britannia PetiteOP
solved checking if pathname equal to "/search" otherwise return;