admin管理员组文章数量:1289542
Considering the code below in .../pages/_app.js, Component is the ponent exported from the current page.
So assuming you visit , the exported ponent in .../pages/about.js will be the value of the Component in .../pages/_app.js
Now when you console log the pageProps, an empty object is displayed.
My question is how do you set the pageProps value in .../pages/about.js for example.
.../pages/about.js
const About = () => {
return <p>About Page</p>
}
export default About
.../pages/_app.js
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
Considering the code below in .../pages/_app.js, Component is the ponent exported from the current page.
So assuming you visit https://yourdomain./about, the exported ponent in .../pages/about.js will be the value of the Component in .../pages/_app.js
Now when you console log the pageProps, an empty object is displayed.
My question is how do you set the pageProps value in .../pages/about.js for example.
.../pages/about.js
const About = () => {
return <p>About Page</p>
}
export default About
.../pages/_app.js
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
Share
Improve this question
asked Apr 6, 2021 at 15:50
claOnlineclaOnline
1,0553 gold badges11 silver badges18 bronze badges
1 Answer
Reset to default 6How To Set pageProps Property In Next.js Page
When you fetch the data using getStaticProps
or getServerSideProps
then you pass props
ie may be the data from api call . Then you pass it via props
to your Page Component
. That props is represented as pageProps
in _app.js
.
In your code here you have not not used data fetching getStaticProps
or getServerSideProps
and passed return value from function as props
. So You are getting empty Object as pageProps
const About = () => {
return <p>About Page</p>
}
export default About
Belowtodos
is passed as props to Page Component
. When you do console.log(pageProps)
in _app.js
then you will get api call result as pageProps
export default function IndexPage(props) {
return (
<div>
Todos list
<ul>
{props.todos.map((todo) => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
</div>
);
}
export async function getServerSideProps() {
const todos = await fetch(
"https://jsonplaceholder.typicode./todos"
).then((response) => response.json());
return {
props: { todos }
};
}
Sandbox Link
本文标签: javascriptHow To Set pageProps Property In Nextjs PageStack Overflow
版权声明:本文标题:javascript - How To Set pageProps Property In Next.js Page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741400716a2376650.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论