我想更新我的状态来控制我的模型,但当我运行此代码时它不起作用。
这些是我控制模型可见性的状态
state = {
loginWithMail: false,
registerWithMail:true,
remember:true,
}
这就是我在函数中尝试过的。
this.setState({
registerWithMail: false,
loginWithMail: true,
})
console.log(this.state)
};
这是我试图控制的模型
<Modal isVisible={this.state.registerWithMail}>
最佳答案
setState
是异步的,因此您需要将 console.log 放入 setState
提供的回调中状态更新后进行记录。
this.setState({
registerWithMail: false,
loginWithMail: true,
}, () => console.log(this.state))
};
此外,我相信 React Native 模式需要此属性,除非这是您创建的自定义组件 <Modal visible={this.state.registerWithMail}>
https://facebook.github.io/react-native/docs/modal.html#visible
关于javascript - setState() 函数不起作用,我无法更新我的状态。我怎么解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650227/