Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NextJs routes with param in between routes

Is there a way in nextjs to create routes like /services/${service.id}/subscriptions?

I have many subroutes where the serviceId is needed. My initial fould would be to create a folder structure like so but this doesn’t work. Any ideas?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Edit: using page router

>Solution :

Create the following file structure inside your pages folder. Note how I omitted the ... from your dynamic route.

pages/
├─ services/
│  ├─ [serviceId]/
│  │  ├─ subscriptions/
│  │  │  ├─ index.tsx
│  │  ├─ index.tsx

Here is an example of how you can access the serviceId inside your page at subscriptions/index.tsx:

import { useRouter } from 'next/router';

const Subscriptions = () => {
  const router = useRouter();
  const { serviceId } = router.query;

  return (
    <div>
      <h1>Subscriptions for Service ID: {serviceId}</h1>
    </div>
  );
};

export default Subscriptions;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading