I have a code scenario where I need get the updated state in the setState callback. Like below :
this.setState({name : "xyz"}, () => {
//I want the updated state here where I need to get the this.state.name should be "xyz"
})
To get the updated state after setting a state is same as described in question.
this.setState({name : "xyz"}, () => {
console.log(this.state.name)
});
I asked this question as I had some issues in my function and was unable to get the updated state.
Use componentDidUpdate() to get updated state and props values.
componentDidUpdate(prevProps, prevState) {
console.log(this.state, prevState);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.