useParams
API Reference for the useParams hook.
useParams is a Client Component hook that lets you read a route's dynamic params filled in by the current URL.
Parameters
useParams does not take any parameters.
Returns
useParams returns an object containing the current route's filled in dynamic parameters.
- Each property in the object is an active dynamic segment.
- The properties name is the segment's name, and the properties value is what the segment is filled in with.
- The properties value will either be a
stringor array ofstring's depending on the type of dynamic segment. - If the route contains no dynamic parameters,
useParamsreturns an empty object. - If used in Pages Router,
useParamswill returnnullon the initial render and updates with properties following the rules above once the router is ready.
For example:
| Route | URL | useParams() |
|---|---|---|
app/shop/page.js | /shop | {} |
app/shop/[slug]/page.js | /shop/1 | { slug: '1' } |
app/shop/[tag]/[item]/page.js | /shop/1/2 | { tag: '1', item: '2' } |
app/shop/[...slug]/page.js | /shop/1/2 | { slug: ['1', '2'] } |
Behavior
Cache Components
When cacheComponents is enabled, useParams may require a Suspense boundary. This depends on whether the params can be resolved during prerendering.
- Static routes and routes with
generateStaticParams: every dynamic param is known at build time.useParamsresolves on the server and noSuspenseboundary is required. - Routes with dynamic params not covered by
generateStaticParams: the param is not known until request time.useParamssuspends. Wrap the component (or a parent) in aSuspenseboundary so its fallback can be rendered during prerendering; otherwise, the build fails.
See Next.js encountered URL data in a Client Component outside of Suspense for full fix options and trade-offs.
Version History
| Version | Changes |
|---|---|
v13.3.0 | useParams introduced. |