我无法在渲染中检查先前状态的值
我的代码是
this.setState(previousState => ({
if(name!=previousState.name){
name:name,
uri:uri,
fbid:fbid,
}
}));
我无法在渲染中检查先前状态的值
我的代码是
this.setState(previousState => ({
if(name!=previousState.name){
name:name,
uri:uri,
fbid:fbid,
}
}));
有一个组件生命周期方法,componentWillUpdate
您可以通过它检查当前和新状态。当 props 或 state 改变时,它总是被调用。例如:
componentWillUpdate(nextProps, nextState) {
console.log(nextState); //will show the new state
console.log(this.state); //will show the previous state
}
如需参考,请访问:componentWillUpdate
handelFilterVisibility = () => {
this.setState(prevState => ({
isFilterVisible: !prevState.isFilterVisible
}));
}
react-native 确实有一个生命周期方法,其中包含您以前的状态以及新状态,您可以将它们与此 LC 方法进行比较。您可以在此处阅读有关生命周期方法的信息 https://reactjs.org/docs/react-component.html
handelFilterVisibility -> 这是一个自定义方法,当有人点击按钮时会调用它 & prevState -> 是前一个状态,所以在这个方法中,我们取前一个状态并更改它的属性“isFilterVisible”