I have this function, I want console.log the output to show the state of isOpen, the expected output must be 'true' only.
open = () => {
this.setState({
isOpen: true
})
}
I have this function, I want console.log the output to show the state of isOpen, the expected output must be 'true' only.
open = () => {
this.setState({
isOpen: true
})
}
Since the setState()
is async, you need to call a callback, which executes after the state gets updates.
open = () => {
this.setState({
isOpen: true
}, () => console.log(this.state.isOpen))
}
this.setState will take a callback function.
open = () => {
this.setState({
isOpen: true
}, () => {
console.log(this.state.isOpen ? this.state.isOpen : '')
}
}
Since setState()
is async, you need to put console.log
in callback of setState
.
open = () => {
this.setState({
isOpen: true
}, ()=> {
console.log(this.state.isOpen);
}