Repository navigation
Description
What version of React Router are you using?
6.7.0
Steps to Reproduce
- Use
setSearchParamsin anything that needs a dependency array and add it to avoid issues. - Notice that the presence of
setSearchParamsin the dependency array triggers the useEffect any time the searchParams change (for example) to run again
Expected Behavior
I expect to be able to use setSearchParams in a useEffect and not have that useEffect re-run just because search params changed.
Actual Behavior
This is happening because setSearchParams allows you to pass a function callback which receives the current searchParams so setSearchParams changes whenever the searchParams change. This is a nice API, but leads to unexpected re-runs of things using setSearchParams.
An alternative is to use a ref to keep track of the latest searchParams and pass that ref.current instead so searchParams doesn't need to be listed in the nextInit call. I can't think of a scenario where this would break anything.
The code I'm talking about is here:
react-router/packages/react-router-dom/index.tsx
Lines 872 to 881 in 0ce0e4c
| let setSearchParams = React.useCallback<SetURLSearchParams>( | |
| (nextInit, navigateOptions) => { | |
| const newSearchParams = createSearchParams( | |
| typeof nextInit === "function" ? nextInit(searchParams) : nextInit | |
| ); | |
| hasSetSearchParamsRef.current = true; | |
| navigate("?" + newSearchParams, navigateOptions); | |
| }, | |
| [navigate, searchParams] | |
| ); |
Activity
timdorr commentedon Jan 27, 2023
Some historical context on this:
#9304
#9912
#8908 / #9851
max-pcentra commentedon Jan 29, 2023
Also, calling
setSearchParamswill re-render the whole route. It would be very helpful if at least there would be some sort of flag on each route config, similar toshouldRevalidate, that let's you opt out of such re-renders.zhengjitf commentedon Feb 7, 2023
I feel that all functions in hooks, especially those we’ll call in other hooks or as props to a component, are supposed to be stable. we just need to be able to get the latest states when these functions are called.
The following is a simple solution to create stable functions:
github-actions commentedon Apr 17, 2023
This issue has been automatically marked stale because we haven't received a response from the original author in a while 🙈. This automation helps keep the issue tracker clean from issues that are not actionable. Please reach out if you have more information for us or you think this issue shouldn't be closed! 🙂 If you don't do so within 7 days, this issue will be automatically closed.
dennisameling commentedon Apr 17, 2023
Please don't close, I literally ran into this issue today 🙏🏼
jamesa3 commentedon Apr 18, 2023
We are still affected by this bug. This issue should not be closed until a fix is published.
KostiantynO commentedon May 16, 2023
I use useCallback and it works!
Please kindly consider to make updateFunction a stable reference :)
maastrich commentedon Jun 1, 2023
Also, if multiple updates happen in the same render cycle, using the new value as a dependency of the callback causes it to no be provided within the prev param of the n+1
setSearchParamscalllucabergamini commentedon Jun 2, 2023
This is what I'm currently using as a workaround (following @zhengjitf stable function suggestion):
in case anybody is looking for the same workaround. It works for my use case, but I've not tested corner cases.
maastrich commentedon Jun 6, 2023
@lucabergamini useEffect is no good, it would be best at the root off the component as the ref would be instantly updated instead of waiting for the render cycle to end in order to run the effects.
lucabergamini commentedon Jun 6, 2023
Do you mean a
setSearchParamsRef.current=setSearchParamsin the body of the hook to replace theuseEffect@maastrich?maastrich commentedon Jun 15, 2023
@lucabergamini yes
15 remaining items
bdwain commentedon Aug 21, 2024
ah i see. mine doesn't work with multiple params as the previous state only knows the the current params the last time that specific piece of state changed
bdwain commentedon Aug 21, 2024
@buzinas i had an issue with this version of the method when i made 2 changes to the url in quick succession in different hooks. They both acted off the same previous state.
The example where this occurred is having a perPage and offset value on a paginated page, and changing the perPage value causes offset to reset back to 0.
My solution was to move the reading of the current query variables as close as possible to the setting of the new ones, such that it's all synchronous, rather than trying to integrate it with the react lifecycle. This removes the option to pass the previous state to the setter, and requires you to pull
document.location.searchdirectly instead.craigmiller160 commentedon Mar 29, 2025
Please for the love of God fix this! Wtf who thought this was a good idea? This is a truly horrible regression and makes using react router more difficult.
kentcdodds commentedon Mar 29, 2025
@craigmiller160 that attitude is unhelpful and unappreciated. If you feel so strongly, make a pull request.
rwilliams3088 commentedon Apr 18, 2025
For the record, Pull Requests have been made and the react-router team has ignored them for over a year (maybe closer to 2 years now). No comments to improve or change anything. So @craigmiller160's exasperation, like all of ours, is justified.
jon-frankel commentedon May 3, 2025
This workaround worked for me. Based on this SO answer.
fix(react-router): make setSearchParams reference stable across renders
fix: make setSearchParams return a stable reference
fix(ui): remove setSearchParams from useEffect deps to break infinite…
fix(ui): remove setSearchParams from useEffect deps to break infinite…
greedyivan commentedon Aug 25, 2026
I went through this thread, the earlier PRs, and the redirect to
useEffectEvent— and there is one fact that decides the shape of the fix, so I'll put it first.The shipped
useEffectEventdoes not have a stable identity. That is not an implementation detail — it is the contract: the React docs say its identity "intentionally changes on every render", and that effect events are only to be called from inside effects — not returned from hooks, not called from event handlers.setSearchParamsis exactly both of those things. SosetSearchParams = useEffectEvent(...)doesn't fix this issue — it churns harder than the current code (a new function every render, instead of every navigation).What the redirect is pointing at is the timing. React's effect-event mechanism reads fresh values because its internal reference is switched during the commit, before layout effects run. That timing is the fix. The upcoming PR reproduces it with documented primitives:
setSearchParamsholds one identity for the lifetime of the hook, and a call — through any reference, captured before or after a navigation — readssearchParamsandnavigatefrom the latest committed render. Event handlers, timeouts, and passive effects always see the latest committed values.This is also the shape every comparable setter already ships:
dispatchfrom react-redux,navigatefrom TanStack Router,refetchfrom TanStack Query — stable reference, current state read at call time.Two things relative to #14785 (which I appreciate — it's what surfaced the redirect): its mirror switches in a passive effect, so there is a window where a captured reference still reads the previous navigation; and its callback keeps
[navigate]in the deps, which churns on pathname changes outside data routers — so the reference isn't stable there. Both are covered: pathname navigations keep the identity, and functional updates read committed values.One boundary is real and documented rather than hidden: a call made from another component's
useLayoutEffectthat flushes earlier in the same commit may see the previous render's values. The platform mechanism has the same boundary — its switch also happens in the commit phase. And the existing queueing caveat stays true: two calls in the same tick don't build on each other; this is a URL setter, not React state.Nine new tests, six of them fail on current
main. PR in a moment.