我刚开始学习reactjs并尝试从API检索数据:
constructor(){
super();
this.state = {data: false}
this.nextProps ={};
axios.get('https://jsonplaceholder.typicode.com/posts')
.then(response => {
nextProps= response;
});
}
当 Promise 返回数据时,我想将其分配给状态:componentWillReceiveProps(nextProps){
this.setState({data: nextProps})
}
我该如何使用从API收到的数据更新状态?目前状态未设置。
setState
,而不通过 props。 - mguijarr