Repository navigation
fix(react-router): make setSearchParams reference stable across renders - #14785
DukeDeSouth wants to merge 4 commits into
Conversation
`setSearchParams` was recreated on every render when search params changed because `searchParams` was listed as a `useCallback` dependency. This broke `useEffect` patterns where `setSearchParams` appeared in the dependency array, causing infinite re-renders or unnecessary effect re-runs. The fix stores `searchParams` in a ref that is updated via `useEffect` (not during render, to stay safe under concurrent rendering). The callback reads from the ref, so `searchParams` can be removed from the `useCallback` dependency array. This makes `setSearchParams` behave like React's `setState` — reference-stable across renders. The ref is updated in an effect rather than during render to comply with React's rules for concurrent mode, where render functions may be called multiple times before committing. This addresses the concern raised in the review of remix-run#10740. Fixes remix-run#9991 Co-authored-by: Cursor <cursoragent@cursor.com>
|
|
Hi @DukeDeSouth, Welcome, and thank you for contributing to React Router! Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once. You may review the CLA and sign it by adding your name to contributors.yml. Once the CLA is signed, the If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at hello@remix.run. Thanks! - The Remix team |
Co-authored-by: Cursor <cursoragent@cursor.com>
|
Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳 |
90a9a3d to
327a59e
Compare
|
Added the changeset, so this should be fully release-ready now. I noticed #14803 was closed as a dup of this one, so happy to rebase or adjust anything if it helps get it across the line. Thanks! |
✅ Change File FoundA change file file exists in this PR. Thanks! |
The repo switched from changesets to the new change-file system, so move the release note into packages/react-router/.changes/ as a patch entry.
|
Migrated the changeset to the new |
|
We should be using const setSearchParams = useEffectEvent((nextInit, navigateOptions) => {
// existing useCallback implementation
}); |
|
Hi! I'd like to share why another fix for this issue appeared, and what cases this one doesn't cover yet. Maybe it will be useful for the review. This PR makes
#15438 was written to close both: the ref is switched in a layout effect (before paint, same phase React itself uses for this), and Would you consider taking the approach from #15438, or the two fixes above into this PR? Happy to help either way. |
Human View
Summary
setSearchParamsreturned byuseSearchParams()is recreated every time search params change becausesearchParamsis in theuseCallbackdependency array. This causes:useEffect(() => { ... }, [setSearchParams])to re-run on every search params changesetSearchParamsas a prop to re-render unnecessarilyThis is a 3-year-old issue reported by @kentcdodds with 77 reactions and 33+ comments.
Solution
Store
searchParamsin a ref and update it viaReact.useEffect. The callback reads fromsearchParamsRef.currentinstead of the closure-capturedsearchParams, allowingsearchParamsto be removed from theuseCallbackdependency array.Why
useEffectand not render-time ref assignment?A previous PR (#10740) was reviewed by @Brendonovich who correctly pointed out that writing to
ref.currentduring render violates React's concurrent mode rules. This PR addresses that concern by updating the ref in an effect, which runs after React commits the render — making it safe under concurrent rendering.Why not
useLayoutEffect?useSearchParamscan run during SSR, whereuseLayoutEffectproduces warnings.useEffectis sufficient becausesetSearchParamsis never called between render and effect execution (it's only called from event handlers or other effects, both of which run after effects).Changes
packages/react-router/lib/dom/lib.tsx(+13/-3): AddsearchParamsRef+useEffect, update callback to read from ref, removesearchParamsfrom depspackages/react-router/__tests__/dom/search-params-test.tsx(+142/-0): Three new tests:setSearchParamsidentity is preserved across rendersuseEffect([setSearchParams])doesn't re-run on params changesetSearchParams(prev => ...)reads current params via refTest plan
setSearchParamsreference equality across rendersuseEffect([setSearchParams])fires once on mount onlyFixes #9991
AI View (DCCE Protocol v1.0)
Metadata
AI Contribution Summary
Verification Steps Performed
Human Review Guidance
searchParamsRef.current,ref.current,packages/react-router/lib/dom/lib.tsxMade with M7 Cursor