[英]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.