【发布时间】:2020-05-29 05:21:00
【问题描述】:
给定一个返回 JSON 数据的 API,例如:
["posts":
{"id":1,
"name":"example",
"date":"exampledate",
"content":"examplecontent",
"author":"exampleauthor"},
{"id":2,
..]
数组的长度未知。
我正在通过 isomorphic-fetch 获取数据,如下所示:
displayPosts.getInitialProps = async function() {
const res = await fetch('.../post');
const data = await res.json();
return{
posts: data.posts
}
}
正在工作(console.log.stringify(data))。
现在我想在我的 displayPosts 页面上显示这些帖子。 因此我正在使用以下 React 组件。
class Posts extends React.Component {
stat = {
// here i don't know how to set the state
}
render() {
return (
// i want to render the data here
);
}
}
导出默认帖子;
问题:我如何设置一个状态,以便我可以整齐地显示我的 displayPosts.js 页面中的每个帖子
<Posts posts={props.Posts}/>
?
【问题讨论】: