Files
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

243 lines
6.9 KiB
Plaintext

---
title: useParams
description: API Reference for the useParams hook in the Pages Router.
---
`useParams` is a hook that lets you read a route's [dynamic params](/docs/pages/building-your-application/routing/dynamic-routes) filled in by the current URL.
```tsx filename="pages/shop/[slug].tsx" switcher
import { useParams } from 'next/navigation'
export default function ShopPage() {
const params = useParams<{ slug: string }>()
if (!params) {
// Render fallback UI while params are not yet available
return null
}
// Route -> /shop/[slug]
// URL -> /shop/shoes
// `params` -> { slug: 'shoes' }
return <>Shop: {params.slug}</>
}
```
```jsx filename="pages/shop/[slug].js" switcher
import { useParams } from 'next/navigation'
export default function ShopPage() {
const params = useParams()
if (!params) {
// Render fallback UI while params are not yet available
return null
}
// Route -> /shop/[slug]
// URL -> /shop/shoes
// `params` -> { slug: 'shoes' }
return <>Shop: {params.slug}</>
}
```
## Parameters
```tsx
const params = useParams()
```
`useParams` does not take any parameters.
## Returns
`useParams` returns an object containing the current route's filled in [dynamic parameters](/docs/pages/building-your-application/routing/dynamic-routes), or `null` during [prerendering](#behavior-during-prerendering).
- Each property in the object is an active dynamic segment.
- The property name is the segment's name, and the property value is what the segment is filled in with.
- The property value will either be a `string` or array of `string`s depending on the [type of dynamic segment](/docs/pages/building-your-application/routing/dynamic-routes).
- If the route contains no dynamic parameters, `useParams` returns an empty object.
For example:
| Route | URL | `useParams()` |
| ---------------------------- | ----------- | ------------------------- |
| `pages/shop/page.js` | `/shop` | `{}` |
| `pages/shop/[slug].js` | `/shop/1` | `{ slug: '1' }` |
| `pages/shop/[tag]/[item].js` | `/shop/1/2` | `{ tag: '1', item: '2' }` |
| `pages/shop/[...slug].js` | `/shop/1/2` | `{ slug: ['1', '2'] }` |
> **Good to know**: `useParams` is a [React Hook](https://react.dev/learn#using-hooks) and cannot be used with classes.
## Behavior
### Behavior during prerendering
For pages that are [statically optimized](/docs/pages/building-your-application/rendering/automatic-static-optimization), `useParams` will return `null` on the initial render. After hydration, the value will be updated to the actual params once the router is ready.
This is because params cannot be known during static generation for dynamic routes.
```tsx filename="pages/shop/[slug].tsx" switcher
import { useParams } from 'next/navigation'
export default function ShopPage() {
const params = useParams<{ slug: string }>()
if (!params) {
// Return a fallback UI while params are loading
// This prevents hydration mismatches
return <ShopPageSkeleton />
}
return <>Shop: {params.slug}</>
}
```
```jsx filename="pages/shop/[slug].js" switcher
import { useParams } from 'next/navigation'
export default function ShopPage() {
const params = useParams()
if (!params) {
// Return a fallback UI while params are loading
// This prevents hydration mismatches
return <ShopPageSkeleton />
}
return <>Shop: {params.slug}</>
}
```
### Using with `getServerSideProps`
When using [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props), the page is server-rendered on each request and `useParams` will return the actual params immediately:
```tsx filename="pages/shop/[slug].tsx" switcher
import { useParams } from 'next/navigation'
export default function ShopPage() {
const params = useParams<{ slug: string }>()
// With getServerSideProps, this fallback is never rendered because
// params is always available on the server. However, keeping
// the fallback allows this component to be reused on other pages
// that may not use getServerSideProps.
if (!params) {
return null
}
return <>Shop: {params.slug}</>
}
export async function getServerSideProps() {
return { props: {} }
}
```
```jsx filename="pages/shop/[slug].js" switcher
import { useParams } from 'next/navigation'
export default function ShopPage() {
const params = useParams()
// With getServerSideProps, this fallback is never rendered because
// params is always available on the server. However, keeping
// the fallback allows this component to be reused on other pages
// that may not use getServerSideProps.
if (!params) {
return null
}
return <>Shop: {params.slug}</>
}
export async function getServerSideProps() {
return { props: {} }
}
```
### Comparison with `router.query`
`useParams` only returns the dynamic route parameters, whereas [`router.query`](/docs/pages/api-reference/functions/use-router#router-object) from `useRouter` includes both dynamic parameters and query string parameters.
```tsx filename="pages/shop/[slug].tsx" switcher
import { useRouter } from 'next/router'
import { useParams } from 'next/navigation'
export default function ShopPage() {
const router = useRouter()
const params = useParams()
// URL -> /shop/shoes?color=red
// router.query -> { slug: 'shoes', color: 'red' }
// params -> { slug: 'shoes' }
// ...
}
```
```jsx filename="pages/shop/[slug].js" switcher
import { useRouter } from 'next/router'
import { useParams } from 'next/navigation'
export default function ShopPage() {
const router = useRouter()
const params = useParams()
// URL -> /shop/shoes?color=red
// router.query -> { slug: 'shoes', color: 'red' }
// params -> { slug: 'shoes' }
// ...
}
```
## Examples
### Sharing components with App Router
`useParams` from `next/navigation` works in both the Pages Router and App Router. This allows you to create shared components that work in either context:
```tsx filename="components/breadcrumb.tsx" switcher
import { useParams } from 'next/navigation'
// This component works in both pages/ and app/
export function Breadcrumb() {
const params = useParams<{ slug: string }>()
if (!params) {
// Fallback for Pages Router during prerendering
return <nav>Home / ...</nav>
}
return <nav>Home / {params.slug}</nav>
}
```
```jsx filename="components/breadcrumb.js" switcher
import { useParams } from 'next/navigation'
// This component works in both pages/ and app/
export function Breadcrumb() {
const params = useParams()
if (!params) {
// Fallback for Pages Router during prerendering
return <nav>Home / ...</nav>
}
return <nav>Home / {params.slug}</nav>
}
```
> **Good to know**: When using this component in the App Router, `useParams` never returns `null`, so the fallback branch will not be rendered.
## Version History
| Version | Changes |
| --------- | ----------------------- |
| `v13.3.0` | `useParams` introduced. |