reactjsnext.js

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.


Solution

  • 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