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.
string | null
Returns the first value of the named parameter.
string[]
Returns all values for the named parameter.
boolean
Returns
true if the parameter exists.IterableIterator<string>
Returns an iterator of all parameter names.
IterableIterator<string>
Returns an iterator of all parameter values.
IterableIterator<[string, string]>
Returns an iterator of all
[name, value] pairs.string
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
