我有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功能做出反应?