A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/nextjs/next-js-dynamic-route-segments/ below:

Next.js Dynamic Route Segments - GeeksforGeeks

Next.js Dynamic Route Segments

Last Updated : 23 Jul, 2025

Dynamic routing is a core feature in modern web frameworks, enabling applications to handle variable paths based on user input or dynamic content. In Next.js 13+, with the introduction of the App Router, dynamic routes are implemented using a folder-based structure inside the app directory.

This article provides a step-by-step guide to implementing dynamic routes using the latest App Router approach, replacing the legacy pages directory system.

What Are Dynamic Route Segments?

Dynamic route segments allow developers to define routes that adapt to variable content. For instance, a blog platform might use URLs like /post/123 or /post/my-article. The segment (123, my-article) is dynamic and handled by bracket syntax:

app/post/[id]/page.js

With this structure, any value in place of [id] is captured and made available via the routing API.

Prerequisites: Steps to Implement Dynamic Routes

Here is a step-by-step guide to creating the routing:

Step 1: Create a New Next.js Project

Use the following command to bootstrap your project:

npx create-next-app@latest gfg

During setup, ensure you select the following options:

Step 2: Create a Dynamic Route Folder

Inside the app directory, create a nested folder structure for your route:

app/
  route/
    [data]/
      page.js
Step 3: Access Dynamic Parameters

Use the useParams() hook provided by next/navigation to access the dynamic value:

JavaScript
// app/route/[data]/page.js

'use client'
import { usePathname, useParams } from 'next/navigation'

export default function DynamicRoute() {
  const pathname = usePathname()
  const params = useParams()

  return (
    <div>
      <h1>GeeksforGeeks</h1>
      <h2>Pathname: {pathname}</h2>
      <h2>Dynamic Parameter: {params.data}</h2>
    </div>
  )
}

In the above example first, we are calling our useRouter() hook and after that, we are displaying the objects of our router in the console.

Step 4: Start the Development Server

Launch your app using:

npm run dev

Visit a route like http://localhost:3000/route/hello, and you'll see:

Output:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4