Next.js Discord

Discord Forum

How do I get a part out of my URL? So on the server?

Answered
Hermit Thrush posted this in #help-forum
Open in Discord
Hermit ThrushOP
I know that there are Dynamic Routes, but I only want to access certain parts of the route. So if I have app/[slug]/[id], I want to access slug AND id. And both in the same page.js. How do I get this to work?
Answered by B33fb0n3
you need to remove the types from this example. So your code should look like this:
export default function Page({params: {slug, id}}) {
  return <div>{slug} {id}</div>
}
View full answer

7 Replies

you want to get the value of slug and id right?
export default function Page({
  params
}: {
  params: { slug: string, id: string }
}) {
  return <div>{slug} {id}</div>
}
Hermit ThrushOP
yes, when I do that, I get the error, that params is not defined...
you need to remove the types from this example. So your code should look like this:
export default function Page({params: {slug, id}}) {
  return <div>{slug} {id}</div>
}
Answer
is your file under app/[slug]/[id]/page.tsx?
@Ray is your file under app/[slug]/[id]/page.tsx?
Hermit ThrushOP
yes