简体   繁体   中英

how to access state value right after setting the same state variable in another function in react.js class component

submit = () => {
   this.checkValidation();
   console.log(this.state.isError) //this is not giving updated value
}

checkValidation = () => { this.setState({ isError: true }) }

You can callback funtion to access the updated state right after setting it,

this.setState({ isError: true }, this.logState)

 logState = () => {
    console.log(this.state.isError)
  };

In your case you can either move the setting of state inside the submit function or return the updated the updated value from checkValidation function and then set the state within submit function:

submit = () => {
   this.checkValidation();
   setState(
    { isError: true },
    () => console.log(this.state.isError)
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM