状态更新后,我需要更新表。因此,我尝试在getDerivedStateFromProps内部调用函数更新,但似乎无法在getDerivedStateFromProps内部使用“ this”。有什么办法吗?预先感谢。
static getDerivedStateFromProps(props, state) {
if(props.newValue != state.newValue) {
this.updateTable();
return {
newValue : props.newValue
}
}
return null;
}
updateTable
是sideEffect
不需要存在的功能getDerivedStateFromProps
getDerivedStateFromProps
是故意写成静态函数的,因此您不能从中调用类实例函数
为了处理sideEffect,您必须在componentDidUpdate函数中执行它们
componentDidUpdate(prevProps, prevState) {
if(this.props.newValue != prevProps.newValue) {
this.updateTable();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句