# Dynamic Segments (useParams)

> Reading [slug] and [id] values from the URL path.

Subject: Next.js | Lesson 41 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/use-params
Previous: [Query Strings (useSearchParams)](https://codewithanimation.in/nextjs/use-search-params.md) | Next: [Layout Segments](https://codewithanimation.in/nextjs/use-selected-layout-segment.md)

## Dynamic Routing

`useParams` is a Client Component hook that lets you read a route's dynamic segments.

Dynamic segments are the parts of your folder structure wrapped in brackets, like `[id]` or `[slug]`.

## Client-Side Access

While Server Components automatically receive `params` as a prop, Client Components deeper in the tree do not.

Instead of manually drilling the `params` prop down through multiple layers of components, any Client Component can simply call `useParams()` to access the current dynamic segments.

## The Returned Object

The hook returns an object where the keys are the folder names (without brackets) and the values are the actual strings from the URL.

For example, visiting `/shop/shoes/nike` in a route structured as `/shop/[category]/[brand]` will return `{ category: 'shoes', brand: 'nike' }`.

## Common mistake: Params vs SearchParams

A very common mistake is confusing this hook with `useSearchParams`.

`useParams` ONLY reads the folder structure (`/blog/123`). It completely ignores query strings (`?id=123`).

## Tip: Catch-All Routes

If you use a catch-all route like `[...slug]`, the returned value for that key will be an array of strings, e.g., `{ slug: ['a', 'b', 'c'] }`.

## Try it: The Object Parser

Select a route structure and click the test URLs. Watch how `useParams` parses the URL string into a usable JavaScript object.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/use-params

## Check yourself

### Question 1

If your file structure is `/app/store/[storeId]/item/[itemId]/page.tsx` and the user visits `/store/55/item/99`, what does `useParams()` return?

- A. `[55, 99]`
- B. `{ storeId: '55', itemId: '99' }`
- C. `{ id: '55/99' }`
- D. `{ query: '?storeId=55&itemId=99' }`

Correct answer: B

The hook parses the URL and maps the values to the exact bracketed folder names defined in your filesystem.

- Why not A: It returns an object, not an array.
- Why not C: It maps keys directly to the folder names.
- Why not D: These are route segments, not query strings.

## Remember this

- `useParams` reads dynamic route segments (folders with `[brackets]`).
- It returns an object mapping the folder name to the URL value.
- It does NOT read query strings (`?key=value`).
- Catch-all routes (`[...slug]`) will return an array of strings.
