Skip to content Skip to sidebar Skip to footer

React State Is Not Assigning Properly With Setstate

I'm creating a very basic todo app. But I'm getting an issue with pushing todos into an array and assigning them to the state. constructor(props){ super(props); this.stat

Solution 1:

setStateis an async operation but it does allow a callback to be triggered when the operation is complete. Change your code to

this.setState({ todo }, () =>console.log(this.state.todo));

and you'll see the latest changes you made to the state.

Solution 2:

setState is async, so react can batch several call to setState in order to improve performance.

As said above, either use the callback function of setState OR the componentLifeCycle componentDidUpdate

Post a Comment for "React State Is Not Assigning Properly With Setstate"