[英]how to pass object as params in Router.push() in nextjs and access that object in other component
[英]how to access router params in getInitialProps stateles component in nextjs
我正在使用 nextjs 並且我在使用 getInitialProps 時遇到問題。
我想使用我在 getInitialProps 的 Post Component 中創建的變量。 你可以看到我的男女同校:
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
}
我該怎么做 ?!!!
您應該能夠從 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.