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的简单用法
最新推荐文章于 2026-06-21 12:07:40 发布
本文深入探讨了React中static getDerivedStateFromProps和componentDidUpdate生命周期方法的使用,讲解了如何根据props变化更新组件状态,确保组件正确响应外部数据变动。
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印
2519

被折叠的 条评论
为什么被折叠?



