[英]How to update the value a component's state variable in another component in React.js?
[英]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.