简体   繁体   中英

how to access router params in getInitialProps stateles component in nextjs

im using nextjs and i have issue in using getInitialProps .

i want to use variables i created in Post Component in getInitialProps. you can see my coed :

const Post = props => {
  const router = useRouter()
   let id = router.query.id  
   let urlTitle = router.query.title

  return (
    <Layout>
       //somthing
    </Layout>
  )
}


Post.getInitialProps = async () => {

// i want to use {id} and {urlTitle} here

}

How can i do it ?!!!

You should be able to access {query} from getInitialProps

const Post = ({id, urlTitle}) => {

  return (
    <Layout>
       //somthing
    </Layout>
  )
}


Post.getInitialProps = async ({ query }) => {

  const { id, title } = query

  return {
    id,
    urlTirle: title
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM