![](/img/trans.png)
[英]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.