static getDerivedStateFromProps(nextProps, state) {
//在getDerivedStateFromProps里不能使用this
if (nextProps.navIndex !== state.navIndex) {
return {
navIndex: nextProps.navIndex
};
}
return null;
}
componentDidUpdate(prevProps, prevState) {
//必须写在if里面并且重新进行一次this.props.navIndex !== prevProps.navIndex的判断
if (this.props.navIndex && (this.props.navIndex !== prevProps.navIndex)) {
this.setState({
result: []
}, () => {
this.getDropArticle(this.state.navIndex, 1)
})
}
}
getDerivedStateFromProps的简单用法
最新推荐文章于 2024-01-29 09:50:41 发布
本文深入探讨了React中static getDerivedStateFromProps和componentDidUpdate生命周期方法的使用,讲解了如何根据props变化更新组件状态,确保组件正确响应外部数据变动。

5420





