简体   繁体   English

如何在nextjs的getInitialProps stateles组件中访问路由器参数

[英]how to access router params in getInitialProps stateles component in nextjs

im using nextjs and i have issue in using getInitialProps .我正在使用 nextjs 并且我在使用 getInitialProps 时遇到问题。

i want to use variables i created in Post Component in getInitialProps.我想使用我在 getInitialProps 的 Post Component 中创建的变量。 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您应该能够从 getInitialProps 访问 {query}

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

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


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

  const { id, title } = query

  return {
    id,
    urlTirle: title
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 nextjs 的 Router.push() 中将 object 作为参数传递并在其他组件中访问该 object - how to pass object as params in Router.push() in nextjs and access that object in other component 在 nextjs 的 Layout 组件中使用 getInitialProps - Use getInitialProps in Layout component in nextjs 如何从路由器组件以外的组件访问路由参数 - How to access route params from a component other then the Router component Nextjs-如何使用Link将参数传递到服务器路由器 - Nextjs - How to pass params to server router with Link 使用 redux 访问 getInitialProps 中的存储在 nextjs 中持续存在 - Access store in getInitialProps with redux persist in nextjs 来自其他组件的React Router访问参数 - React Router access params from another component 如何在功能组件的 getInitialprops 中访问特定页面的道具? - How do I access props for a particular page in getInitialprops for a functional component? nextjs redux,thunk和getInitialProps - 如何实现 - Nextjs redux, thunk and getInitialProps - how to implement 如何将自定义参数传递给 nextjs 动态路由,以便我可以在 getInitialProps 中访问它 - How do I pass a custom parameter to nextjs dynamic route so I can access it inside getInitialProps 如何在 React 组件中使用 getInitialProps - How to use getInitialProps in React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM