# Dynamic Routes ([slug])

> Capturing URL parameters to build dynamic pages.

Subject: Next.js | Lesson 14 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/dynamic-routes
Previous: [The default.js Convention](https://codewithanimation.in/nextjs/default-file.md) | Next: [Catch-all Routes ([...slug])](https://codewithanimation.in/nextjs/catch-all-routes.md)

## Bracket notation

When you are building an e-commerce site, you can't manually create a folder for every single product (e.g., `/product/1`, `/product/2`, etc.).

Instead, Next.js uses Dynamic Routes. By wrapping a folder name in square brackets—such as `[id]` or `[slug]`—you tell the router to capture that segment of the URL as a dynamic parameter.

## The params prop

When a user visits `/blog/my-first-post`, and your folder structure is `app/blog/[slug]/page.tsx`, Next.js routes the request to that page.

Crucially, it extracts the value `'my-first-post'` and passes it directly into your `page.tsx` component via a prop called `params`.

## The Next 15 Promise Rule

Here is the most critical thing to know for modern Next.js (v15+): **the `params` prop is a Promise**.

Because extracting parameters from the request happens asynchronously during the server rendering lifecycle, you must `await` the params object before you can read from it: `const { slug } = await params;`.

## Common mistake: Synchronous crashes

In older versions of Next.js, `params` was a simple synchronous object. Many old tutorials and ChatGPT responses will tell you to write `export default function Page({ params: { id } })`.

If you do this in Next.js 15, it will throw a warning or crash. You must treat `params` as an async promise on the server, or use React's `use()` hook on the client.

## Tip: Multiple dynamic segments

You aren't limited to one dynamic parameter per route.

You can nest them: `app/[category]/[productId]/page.tsx`. If the user visits `/shoes/air-jordans`, your `params` promise will resolve to `{ category: 'shoes', productId: 'air-jordans' }`.

## Try it: Parameter Extraction

Type a custom user ID into the URL bar and hit enter. Watch how the Next.js router extracts the segment, resolves the Promise, and injects it into the Page component.

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

## Check yourself

### Question 1

How do you define a dynamic route segment for a user ID in the App Router?

- A. Create a folder named :id
- B. Create a folder named [id]
- C. Export a dynamic config from page.tsx
- D. Create a folder named {id}

Correct answer: B

Next.js uses square brackets `[paramName]` to denote a dynamic route segment.

- Why not A: That is Express/React Router syntax.
- Why not C: Dynamic routes are defined by folder structure, not config.
- Why not D: Next.js uses square brackets.

### Question 2

In modern Next.js (v15+), how must you access the 'id' parameter inside a Server Component?

- A. const id = params.id
- B. const id = await params.id
- C. const { id } = await params
- D. const id = useRouter().query.id

Correct answer: C

In Next.js 15, `params` is a Promise. You must `await` it before destructuring its properties.

- Why not A: This was Next.js 14. In 15, params is a Promise.
- Why not B: You await the params object, not the property.
- Why not D: useRouter().query is Pages Router syntax.

## Remember this

- Wrap a folder name in brackets `[id]` to create a dynamic route.
- The extracted value is passed to the component via the `params` prop.
- In Next.js 15+, `params` is a **Promise**. You must `await` it.
- You can nest multiple dynamic segments in a single route.
