useSearchParams is a Client Component hook that returns a read-only version of the URLSearchParams interface for the current URL query string.
app/dashboard/search-bar.tsx
Parameters
useSearchParams takes no parameters.
Returns
A read-onlyURLSearchParams instance.
Returns the first value of the named parameter.
| URL | searchParams.get('a') |
|---|---|
/dashboard?a=1 | '1' |
/dashboard?a= | '' |
/dashboard?b=3 | null |
/dashboard?a=1&a=2 | '1' |
Returns all values for the named parameter.
Returns
true if the parameter exists.| URL | searchParams.has('a') |
|---|---|
/dashboard?a=1 | true |
/dashboard?b=3 | false |
Returns an iterator of all parameter names.
Returns an iterator of all parameter values.
Returns an iterator of all
[name, value] pairs.Returns the query string as a string (without the leading
?).Good to know
useSearchParamsis a Client Component hook and is not supported in Server Components, to prevent stale values during partial rendering.- In Server Component pages, read the
searchParamsprop instead. - Layouts do not receive
searchParamsbecause they are not re-rendered on navigation. - If your app has a
/pagesdirectory,useSearchParamsmay returnnullon initial render for pages that don’t usegetServerSideProps.
Behavior
Prerendering and Suspense
If a route is prerendered, callinguseSearchParams causes the Client Component tree up to the closest Suspense boundary to be client-side rendered. Wrap the component in <Suspense> to allow the rest of the page to prerender normally:
app/dashboard/page.tsx
Dynamic rendering
If the route is dynamically rendered (e.g. via theconnection function), useSearchParams is available on the server during the initial render:
app/dashboard/page.tsx
Examples
Updating search params
UseuseRouter or <Link> to set new search params:
app/example-client-component.tsx
Version history
| Version | Changes |
|---|---|
v13.0.0 | useSearchParams introduced. |
