Reputation: 77
I cant check a value of a previous state in render
my code is
this.setState(previousState => ({
if(name!=previousState.name){
name:name,
uri:uri,
fbid:fbid,
}
}));
Upvotes: 1
Views: 10797
Reputation: 2788
I think we can answer this in a more detailed and recent way:
const MyVideoPlayer = () => {
const [isPlaying, setIsPlaying] = useState(false);
return (
<View>
<!--...my video player...-->
<TouchableOpacity onPress={() => setIsPlaying(prevState => !prevState)}>
<!-- My setIsPlaying is returning me the previous state in "prevState"
and me in turn returning it's negation. So every time the user klicks the
button it will change from play to pause -->
{isPlaying ? <PauseButton /> : <PlayButton />}
</TouchableOpacity>
</View>
);
};
Upvotes: 0
Reputation: 1013
handelFilterVisibility -> this is a custom method which will invoke when someone clicks a button & prevState -> is the previous state, so in this method, we take the previous state and change it's property 'isFilterVisible'
Upvotes: 0
Reputation: 1013
handelFilterVisibility = () => {
this.setState(prevState => ({
isFilterVisible: !prevState.isFilterVisible
}));
}
Upvotes: 2
Reputation: 151
react-native do have a lifecycle method for this which contains your previous state as well as new state you can compare both of them into this LC method. you can read about lifecycle methods over here https://reactjs.org/docs/react-component.html
Upvotes: 0
Reputation: 2528
There is a component lifecycle method,componentWillUpdate
by which you can check the current and the new state. It is always called when the props or state changes. For example:
componentWillUpdate(nextProps, nextState) {
console.log(nextState); //will show the new state
console.log(this.state); //will show the previous state
}
For reference, visit: componentWillUpdate
Upvotes: 5