繁体   English   中英

如何在 react.js ZA2F2ED4F8EBC2ABC61ZDC4 中的另一个 function 组件中设置相同的 state 变量后立即访问 state 值

[英]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 }) }

您可以在设置后立即回调函数以访问更新的 state,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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