我们可以在React无状态组件中设置异步类型吗?

时间:2016-11-14 09:56:10

标签: javascript reactjs promise async-await

我有React无状态功能如下:

  const func = () => {
    const fetchedPosts = getPost();

    const posts = fetchedPosts.map((item, i) =>
      <Post {...item} />
    );
  return (
    <div>
     {posts}
    </div>
  );
 }

但是当我运行代码时,我在控制台中出现以下错误:

fetchedPosts undefined
Uncaught (in promise) TypeError: Cannot read property 'map' of 

因为getPost()是一个promise而且react组件不会等到promise被解决。

我尝试使用

 const func = async () => {
   const fetchedPosts = await getPost();
   ...

但得到同样的错误。

是否会对组件支持async / await功能做出反应?

0 个答案:

没有答案