React setState not updating state React setState not updating state reactjs reactjs

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');}); 


setState is asynchronous. You can use callback method to get updated state.

changeHandler(event) {    this.setState({ yourName: event.target.value }, () =>     console.log(this.state.yourName)); }


Using async/await

async changeHandler(event) {    await this.setState({ yourName: event.target.value });    console.log(this.state.yourName);}