Replies: 7 comments
-
Would be awesome to merge #8344 I'm missing this functionality as well. Coming from v5 and using |
Beta Was this translation helpful? Give feedback.
-
Do you have any news about this feature ? |
Beta Was this translation helpful? Give feedback.
-
Adapting @zmthy snippet, we can make it work using function useSearchParam(key: string) {
const [search, setSearch] = useSearchParams();
const setter = useCallback((value) => {
const params = createSearchParams(search);
params.set(key, value);
setSearch(params);
}, [search, setSearch, key]);
return [createSearchParams(search).get(key), setter];
}; |
Beta Was this translation helpful? Give feedback.
-
@zmthy My solution to this is:
This spreads the object created from the searchParams key-value pairs into the new setSearchParams object. Note that Object.fromEntries is not IE compatible. |
Beta Was this translation helpful? Give feedback.
-
Thanks to #7586, you can now solve this problem with the You can then reference the Assuming the export function useSearchParamsUpdate(): [
URLSearchParams,
Dispatch<(params: URLSearchParams) => URLSearchParamsInit>
] {
const [params, setParams] = useSearchParams();
return [
params,
useCallback(
(update) =>
setParams(update(new URLSearchParams(history.location.search))),
[setParams]
),
];
} This can be used to implement I'm not sure if it's reasonable to close the issue just yet, because it's inconvenient that you have to set up the router with your own managed history instead of using browser router, and the history router interface is still unstable. Ideally there would be a way to at least get a location ref with a stable identity and a mutable reference to the current location across any of the routers. |
Beta Was this translation helpful? Give feedback.
-
I'm going to convert this to a discussion so it can go through our new Open Development process. Please upvote the new Proposal if you'd like to see this considered! |
Beta Was this translation helpful? Give feedback.
-
I have found myself trying to create basically the same state syncing convenience hook. The propositions using The doc for the OPs solution returns a 404 in the current version. Is this because support for it is no longer desired? If not, are we trying to go about state -> search param syncing the wrong way? Is there a better / easier way to do this that can leverage what I personally agree with the OP that it would be intuitive if |
Beta Was this translation helpful? Give feedback.
-
What is the new or updated feature that you are suggesting?
It would be great if the setter function returned by
useSearchParams
could accept a function that takes the current search params and builds a new one, like the setter returned byuseState
.The input type of the first argument of the setter would be expanded from
URLSearchParamsInit
toSetStateAction<URLSearchParamsInit>
.The resulting function you get from the
useCallback
in the example above now has a stable identity, and does not have to change asparams
changes in order to preserve search params other than"abc"
.Why should this feature be included?
I'm struggling to recreate functionality I had in v5 to implement a hook that manages the value of single search parameter rather than the entire set of search params, so different hooks and components can simultaneously manage parts of the search params without interfering with each other (and without having to reconstruct the rest of the search state when setting its parameter to a new value). A trimmed down implementation of this functionality in v5 looked like this:
The key part of this hook was the appearance of
history.location.search
to construct an up to date view of the search params in the setter. Using this meant two things:search
does not need to be in the deps array ofuseCallback
, so the identity of the setter does not change when the location does.history.location.search
is always up to date.The second point is a crucial one: if a single event calls multiple setters of different
useSearchParam
hooks, using the value ofsearch
fromuseLocation
will mean each change gets reset by each subsequent setter, so only the last one will take effect. Using the mutable value ofhistory.location.search
allows each change to be preserved in the changes that the following setters take.I've managed to implement the same behaviour in v6 by fetching the history out of the context:
This relies on knowledge about the internals of the library that are intentionally not exposed, for good reason.
If
setParams
can provide the current value of the search params when you call it, then both of these problems are solved:params
need not be in the deps array of auseCallback
in order to avoid touching other parameters, and if the given params always reflect previous changes in the same event then they will be preserved in the resulting transformations.Beta Was this translation helpful? Give feedback.
All reactions