useSearchParams
API Reference for the useSearchParams hook in the Pages Router.
useSearchParams is a hook that lets you read the current URL's query string.
useSearchParams returns a read-only version of the URLSearchParams interface.
Parameters
useSearchParams does not take any parameters.
Returns
useSearchParams returns a read-only version of the URLSearchParams interface, or null during pre-rendering.
The interface includes utility methods for reading the URL's query string:
-
URLSearchParams.get(): Returns the first value associated with the search parameter. For example:URL searchParams.get("a")/dashboard?a=1'1'/dashboard?a=''/dashboard?b=3null/dashboard?a=1&a=2'1'- usegetAll()to get all values -
URLSearchParams.has(): Returns a boolean value indicating if the given parameter exists. For example:URL searchParams.has("a")/dashboard?a=1true/dashboard?b=3false -
Learn more about other read-only methods of
URLSearchParams, including thegetAll(),keys(),values(),entries(),forEach(), andtoString().
Good to know: useSearchParams is a React Hook and cannot be used with classes.
Behavior
Behavior during pre-rendering
For pages that are statically optimized (not using getServerSideProps), useSearchParams will return null during pre-rendering. After hydration, the value will be updated to the actual search params.
This is because search params cannot be known during static generation as they depend on the request.
Using with getServerSideProps
When using getServerSideProps, the page is server-rendered on each request and useSearchParams will return the actual search params immediately:
Examples
Updating search params
You can use the useRouter hook to update search params:
Sharing components with App Router
useSearchParams from next/navigation works in both the Pages Router and App Router. This allows you to create shared components that work in either context:
Good to know: When using this component in the App Router, wrap it in a <Suspense> boundary for static rendering support.
Version History
| Version | Changes |
|---|---|
v13.0.0 | useSearchParams introduced. |