Stable useSearchParams for specified keys #14581
Noyabronok
started this conversation in
Proposals
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
When a component updates the query string, all instances of
useSearchParamsreturn a newsearchParamsobject, triggering dependency array hooks like 'useEffect'. This is good when the code cares about the changed parameters, but it's undesirable when the code doesn't. Considering that multiple components can maintain their state in the query string, developers must remember to add additional logic to each one to check if the keys they care about have actually changed, thereby avoiding any negative side effects downstream.Proposed solutions
Option A - update useSearchParams
Update the
useSearchParamsfunction tofilterKeys).filterKeysare provided - filter the searchParameters for those keys. Let's call that internal variablefilteredParamsfilteredParamsbased on a sorted deep comparison.filteredParamseither instead of searchParams or as a 3rd tuple element.Option B - add useFilterSearchParams
If messing around with the
useSearchParamsis too much, introduce a new hookuseFilterSearchParams. It would have the same features as described above, but without the baggage of backwards support foruseSearchParamsapi.Example implementation for option B
Beta Was this translation helpful? Give feedback.
All reactions