小编i a*_*ood的帖子

getInitialProps 不适用于异步函数

我在这个路径中有文件,./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 不是空的

我已按照逐行实现帖子页面中 …

javascript reactjs next.js react-props

5
推荐指数
1
解决办法
1131
查看次数

标签 统计

javascript ×1

next.js ×1

react-props ×1

reactjs ×1