Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

react state value not updating in function

//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');
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #react #state #updating #function
ADD COMMENT
Topic
Name
8+5 =