Course contentsShow
← React
Lesson 1888 of 2,39643. RoutingFree lesson

Dynamic Routes and Route Parameters

Creating dynamic segments with [id] folders and accessing params in Server Components vs Client Components.

Dynamic Routes and Route Parameters

What you'll learn: How to create dynamic URL segments in Next.js using folder conventions and access those parameters in both Server and Client Components.

The Core Idea

When building applications, you often need pages that follow a pattern—like /products/123, /products/456, etc. Instead of creating a separate file for every possible ID, you use dynamic routes. In Next.js App Router, you create these by wrapping a folder name in square brackets: [id], [slug], [productId], etc.

Think of it like a template: the bracket notation tells Next.js "this part of the URL can be anything—capture it and make it available to me."

Creating Dynamic Segments

app/
  products/
    [id]/
      page.tsx    // Matches /products/123, /products/abc, etc.

The folder name [id] creates a dynamic segment. Whatever appears in that URL position becomes accessible as params.id.

Accessing Parameters

In Server Components (the default), params are passed as props:

export default function ProductPage({ params }: { params: { id: string } }) {
  return <h1>Product ID: {params.id}</h1>;
}

In Client Components, you must use the useParams hook from next/navigation:

'use client';
import { useParams } from 'next/navigation';

export default function ClientProductPage() {
  const params = useParams();
  return <h1>Product ID: {params.id}</h1>;
}

Server Components receive params directly because they render once per request on the server. Client Components need a hook because they hydrate in the browser and need runtime access to the URL state.

Key Takeaway: Dynamic routes use [paramName] folder conventions; Server Components receive params as props automatically, while Client Components must call useParams() to access them.