setState() 函数不起作用,我无法更新我的状态。我怎么解决这个问题?

setState() function doesn't work and I cannot update my states. How can I solve this problem?

我想更新我的状态以控制我的模型,但是当我 运行 此代码时它不起作用。

这些是我控制模型可见性的状态

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 需要这个 属性 除非那是你创建的自定义组件 <Modal visible={this.state.registerWithMail}>

https://facebook.github.io/react-native/docs/modal.html#visible