Home / Snippets / How to Access Next.js 2 Route Params
How to Access Next.js 2 Route Params cover

How to Access Next.js 2 Route Params

210

2 years ago

0 comments

In Next.js you can pass and access one or more property route params by the dynamic parameter name that you have set on. For example, pages/post/[postId]/[commentId].js would match /post/post-1/comments/comment-1.
pages/post/[postId]/[commentId].js => /post/post-1/comments/comment-1
Its query object would be:
{
    postId: 'post-1',
    commentId: 'comment-1'
}
your Link component should be like this:
<Link
    href="/post/[postId]/comments/[commentId]"
    as={`/post/${postId}/comments/${commentId}`}
>
    <a>Link to comment</a>
</Link>
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this