Languages
[Edit]
EN

Next.js - get dynamic route segment value

0 points
Created by:
Kenya-Spears
860

In this article, we would like to show you how to get dynamic route segments in Next.js.

Route dynamic segments getting under Next.js
Route dynamic segments getting under Next.js

Practical example

Edit

In order to access dynamic route segments, we need to use the useRouter() hook provided by the next/router module. With the useRouter() hook we can access router.query that contains parsed query string parameters.

Below, you can see the example of how to get a dynamic segment with the name id by destructuring it from the query object:

Note:

The dynamic segments are passed as strings.

Project structure

Edit

Example components

Edit

product/index.js:

product/[id].js:

Alternative titles

  1. Next.js - get router query parameters
  2. Next.js - get query dynamic route params
  3. Next.js - get route query dynamic path segments
  4. Next.js - get route query dynamic path parameters
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join