setState()
is async (React docs), so the state changes won’t be applied immediately. If you want to log out the new state,setState()
takes in a function as the second argument and performs that function when the state is updated. So:
this.setState({
abc: xyz
},
() => console.log(this.state.abc),
)
Or you can also use componentDidUpdate()
, which is recommended