React setState not Updating Immediately

You should invoke your second function as a callback to setState, as setState happens asynchronously. Something like: this.setState({pencil:!this.state.pencil}, myFunction) However in your case since you want that function called with a parameter you’re going to have to get a bit more creative, and perhaps create your own function that calls the function in the props: … Read more

Updating an object with setState in React

There are multiple ways of doing this, since state update is a async operation, so to update the state object, we need to use updater function with setState. 1- Simplest one: First create a copy of jasper then do the changes in that: this.setState(prevState => { let jasper = Object.assign({}, prevState.jasper); // creating copy of … Read more

React setState not updating state

setState() is usually asynchronous, which means that at the time you console.log the state, it’s not updated yet. Try putting the log in the callback of the setState() method. It is executed after the state change is complete: this.setState({ dealersOverallTotal: total }, () => { console.log(this.state.dealersOverallTotal, ‘dealersOverallTotal1’); });