Accessing slug
Answered
Capple posted this in #help-forum
CappleOP
I have a page at /products/[productId]/page.tsx.
How can I access the slug inside the page component?
I found this in the docs but it's just returning undefined
How can I access the slug inside the page component?
I found this in the docs but it's just returning undefined
export default function Page({ params }: { params: { slug: string } }) {
console.log('slug is:', params.slug)
}7 Replies
CappleOP
Bump
Share your files structure
English Lop
Instead params.slug, use params.productId ?
Answer
@Capple I have a page at /products/[productId]/page.tsx.
How can I access the slug inside the page component?
I found this in the docs but it's just returning undefined
export default function Page({ params }: { params: { slug: string } }) {
console.log('slug is:', params.slug)
}
...
export default function Page({ params }: { params: { productId: any }}) {
console.log(params.productId)
}try that
@Capple I have a page at /products/[productId]/page.tsx.
How can I access the slug inside the page component?
I found this in the docs but it's just returning undefined
export default function Page({ params }: { params: { slug: string } }) {
console.log('slug is:', params.slug)
}
export default function Page({ params }: { params: { productId: string } }) {
return <div>My Post: {params.productId}</div>
}Instead of slug it should be whatever you put instead the block brackets "[ ]"
CappleOP
Thank you everyone! The productId worked. I feel dumb as hell now that I didn't think of it myself, i was under the assumption it had to be named slug for some reason.