简体   繁体   中英

getInitialProps does not work with async function

I have file in this path ./pages/blog/[id]/[title].js and here are the codes that I have written:

import React from 'react';
import PropTypes from 'prop-types';
import fetch from 'isomorphic-unfetch';
import BASE_URL from '../../../BaseURL';


const BlogSinglePage = props => {
  console.log(props.post);//undefined
  console.log(props);//{}
 return (
   <div></div>
 );
};

BlogSinglePage.propTypes = {
  post: PropTypes.object
};

BlogSinglePage.getInitialProps = async ({ query }) => {
    const res = await fetch(`${BASE_URL}/api/post/${query.id}`);
    const post = await res.json();
    return { post };
};

export default BlogSinglePage;

my problem is when getInitialProps is an async function, BlogSinglePage 's props is empty but whenever getInitialProps is not an async function, everything works and props is not empty

I have followed the code in Implement the Post Page line by line but it's not working for me

I have no idea why this was the problem,

but i reinstall next js with next-cli and the problem was solved:)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM