Next.js Discord

Discord Forum

Can't get dynamic routing to work

Answered
Saltwater Crocodile posted this in #help-forum
Open in Discord
Saltwater CrocodileOP
I'm trying to make a dynaimc route.
I can access /user,/user/activities and /user/activities/1 However, on the last one I was expecting to be able to get the activityId in the params like this, however params doesn't contain anything like an activityId ?

import React from "react";
import { withPageAuthRequired } from "@auth0/nextjs-auth0";

export default function ActivityPage({ params }) {

    const activityId = params.activityId

    return (
        <div>
            <h1>Activiteis IndexPage</h1>
        </div>
    );
}
export const getServerSideProps = withPageAuthRequired();


pages
├── api
│   ├── activities.ts
│   ├── handler.ts
│   └── hello.ts
├── _app.tsx
├── index.tsx
├── page.tsx
└── user
    ├── activities
    │   ├── [activityId]
    │   │   └── index.tsx
    │   └── index.tsx
    └── index.tsx
Answered by Saltwater Crocodile
const router = useRouter() was the answer......
View full answer

3 Replies

Saltwater CrocodileOP
const router = useRouter() was the answer......
Answer
Skipjack tuna
Yup, as you are using Pages router, nor App one
 const router = useRouter()
  return <p>Data: {router.query.activityId}</p>