Posts Learn Components Snippets Categories Tags Tools About
/

How to Access Next.js 2 Route Params

Get to know how to easily access 2 route params in Next.js to get the paramets and perform an action

Created on Feb 13, 2022

99 views

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>

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

Load comments for How to Access Next.js 2 Route Params

)