我是新来reactjs框架和我有一点困惑,如果是什么在听取正确的方式,如果一个API调用后状态改变。收听时通过Redux的状态变化 - ReactJS
使用then
和catch
调用一个action
经由componentDidUpdate
后:
componentDidMount(){
this.props.getHero(this.props.params.id).then((result) => {
this.props.initialize({
"name":result.name,
"description": result.description
});
})
.catch(error => {
});
}
或经由componentWillUpdate
// Call the getHero action (API)
componentDidMount(){
this.props.getHero(this.props.params.id);
}
// Then listen if the state change via `mapToStateProps`
componentDidMount(){
this.props.getHero(this.props.params.id);
}
componentWillUpdate(){
this.props.initialize({
"name":this.props.heroes.name,
"description": this.props.heroes.description
});
}
这是什么都与终极版呢? – Pavlo
@Pavlo与componentWillUpdate第二块,我从我的行动派遣一个数据我通过API获取的数据经过这么componentWillUpdate将触发因为道具通过mapStateToProps改变。这就是为什么我问是否正确?我可以问你的建议吗? –