Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Parameter from Dynamic Route in Next.js

I am new to Next.js, and setting up my routing mechanism according to documentation by arranging file structuring as : pages/questions/[id].js

I would like to know if there is cool/easier way to get matched id on my page; like in React Router (match.params.id). I don't want to parse window.location.pathname as my first option.

like image 579
Kerem atam Avatar asked Oct 31 '25 18:10

Kerem atam


1 Answers

OK, It was just in beginning of the documentation, my bad i did't read :

import { useRouter } from 'next/router'

const Post = () => {
  const router = useRouter()
  const { pid } = router.query

  return <p>Post: {pid}</p>
}

export default Post
like image 134
Kerem atam Avatar answered Nov 03 '25 10:11

Kerem atam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!