skip to Main Content

I am currently learning next and I am confused as to how to implement dynamic routing.
Here is what I am doing:
enter image description here
Error

"use client"
import React from "react";
import { useRouter } from "next/router";

const ID = () => {
  const router = useRouter();

  return <div>{router.query.id}</div>;
};

export default ID;

Some resources claim that the dynamic route,name [], should be in the name of the folder. Other, like the documentation, claim it should be the file. How to make dynamic routes work in Next.js?

2

Answers


  1. for dynamic routes in nextjs, the parent folder is in brackets while there is a page.tsx in the folder. As you are using nextjs 13. Previous versions of Nextjs dynamic routes could be handled with your routing.

    Solution:check/[id]/page.tsx., the parent folder is in brackets while there is a page in the folder

    Login or Signup to reply.
  2. Create one more folder in your check folder [id] and then create page.tsx inside of the folder and add the following code:

    "use client"
    import React from "react";
    import { useRouter } from "next/router";
    
    const ID = ({params}) => {
      const {id} = params;
    
      return <div{id}</div>;
    };
    
    export default ID;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search