getInitialProps 不适用于异步函数

i a*_*ood 5 javascript reactjs next.js react-props

我在这个路径中有文件,./pages/blog/[id]/[title].js这里是我写的代码:

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;
Run Code Online (Sandbox Code Playgroud)

我的问题是什么时候getInitialProps 是一个async函数,它BlogSinglePage的 props 是空的,但是当getInitialProps 它不是一个async函数时,一切正常,props 不是空的

我已按照逐行实现帖子页面中的代码进行操作,但它对我不起作用

i a*_*ood 0

我不知道为什么这是问题所在

但我重新安装了下一个js next-cli,问题就解决了:)