In my index page like this:
function Index({posts}) {
console.log(posts);
return (
<div>
Homepage
{/* {posts && posts.length > 0 && posts.map(post => <h1 key={post._id}>{ post.title}</h1>)} */}
</div>
)
}
Index.getInitialProps = async ctx => {
try {
const res = await axios({
method: "GET",
url: 'https://jsonplaceholder.typicode.com/posts'
})
return {posts: res.data}
} catch (error) {
return {errorLoading: true}
}
}
export default Index;
console.log(res.data)
work successfully. But I only get console.log(posts)
undefined. why?
I use custom app. I forgot add pageProps in _app.js. After see https://nextjs.org/docs/advanced-features/custom-app I fix these. Thanks for everyone to help me
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.