useSearchParams
Returns a tuple of the current URL’sURLSearchParams and a function to update them. Setting the search params causes a navigation.
Signature
Parameters
URLSearchParamsInit
Optional default search params. This will not change the URL on the first render.Can be:
- A search param string (e.g.,
"?tab=1") - A shorthand object (e.g.,
{ tab: "1" }) - An object with array values (e.g.,
{ brand: ["nike", "reebok"] }) - An array of tuples (e.g.,
[["tab", "1"]]) - A
URLSearchParamsobject
Returns
URLSearchParams
The current search params as a
URLSearchParams object. This is a stable reference that won’t change between renders.SetURLSearchParams
A function to update the search params and navigate to the new URL.
URLSearchParamsInit | function
The new search params or a function that receives the current params and returns new params.
Optional navigation options like
replace, state, preventScrollReset, etc.Usage
Basic usage
Set search params
Multiple ways to set search params:Function callback
Update search params based on current values:Navigation options
Pass navigation options as the second argument:Read search params
Default search params
Provide default values that won’t change the URL initially:Form filters
Tab navigation
Preserve other params
Clear search params
Important Notes
Stable reference
searchParams is a stable reference, safe to use in useEffect dependencies:
Mutability warning
ThesearchParams object is mutable. If you change it without calling setSearchParams, values will change between renders but the URL won’t update:
Type Safety
Create a type-safe wrapper:Related
useLocation- Access current locationuseNavigate- Navigate programmatically<Form>- Submit forms with search params