我想通过fetch请求服务端数据,然后在渲染组件,但是fetch返回的是promise,这个是异步的所以在数据没有准备好的前提下我的组件已经渲染了,所以如何让我的数据准备好之后再渲染组件!
React 组件渲染和fetch请求数据问题
RookieBomb
#1
piscium2010
#2
试着在 render 方法里这样写
render(){
return this.state.blogList.length === 0 ? null :
<div>anything</div>
}
1 Like
xiaotuni
#3
render(){
const {blogList} = this.state;
return (
<div>
{
blogList && blogList.length > 0 && blogList.map((item,index)=>{
<div key = {index}>{item}</div>
});
}
</div>
);
}
试试这样呢