簡體   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