OptionaldefaultInit: URLSearchParamsInitYou can initialize the search params with a default value, though it will not change the URL on the first render.
// a search param string
useSearchParams("?tab=1");
// a shorthand object
useSearchParams({ tab: "1" });
// object keys can be arrays for multiple values on the key
useSearchParams({ brand: ["nike", "reebok"] });
// an array of tuples
useSearchParams([["tab", "1"]]);
// a `URLSearchParams` object
useSearchParams(new URLSearchParams("?tab=1"));
A tuple of the current URLSearchParams
and a function to update them.
Returns a tuple of the current URL's
URLSearchParamsand a function to update them. Setting the search params causes a navigation.setSearchParamsfunctionThe second element of the tuple is a function that can be used to update the search params. It accepts the same types as
defaultInitand will cause a navigation to the new URL.It also supports a function callback like React's
setState:setSearchParamsdoes not support the queueing logic that React'ssetStateimplements. Multiple calls tosetSearchParamsin the same tick will not build on the prior value. If you need this behavior, you can usesetStatemanually.Notes
Note that
searchParamsis a stable reference, so you can reliably use it as a dependency in React'suseEffecthooks.However, this also means it's mutable. If you change the object without calling
setSearchParams, its values will change between renders if some other state causes the component to re-render and URL will not reflect the values.