Currently, doing setSearchParams(/* an object */)
will replace the existing search params. If I would like to extend the search params instead of replacing them, e.g. adding a new param. What will be the most elegant way to achieve that?
I know I can modify the searchParams
directly, then do something like setSearchParams(newSearchParamsToObject)
. Yet, since searchParams
is not an object but a URLSearchParams
. The code will be long and ugly.
Edit: Since some one appears don't understand what I have posted
Currently if we do something like this:
const [searchParams, setSearchParams] = useSearchParams({ a: 1 });
setSearchParams({ b: 2 });
It will replace the searchParams
with ?b=2
.
What I would like to achieve is adding b=2
to existing search params, i.e. the final search params should be ?a=1&b=2
.
setSearchParams([...searchParams.entries(), ['b', 2]]);
it will append the value and you will end up with ?b=2?b=2?b2 (if for example you change the paramas pressing a button for example) – Fomentation