Atakan
Atakan

Reputation: 97

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

I want to update my states for controlling my Models, but it doesn't work when I run this code.

These are my states which are controlling the model's visibility

state = {
    loginWithMail: false,
    registerWithMail:true,
    remember:true,
  }

That's what I tried in function.

    this.setState({
        registerWithMail: false,
        loginWithMail: true,
      })
      console.log(this.state)
  };

And this is my model which is I tried to control

 <Modal isVisible={this.state.registerWithMail}>

Upvotes: 0

Views: 119

Answers (1)

Matt Aft
Matt Aft

Reputation: 8936

setState is async, so you would need to put the console.log into the callback provided by setState to log once the state is updated.

    this.setState({
        registerWithMail: false,
        loginWithMail: true,
      }, () => console.log(this.state))
  };

Also I believe react native modal needs this property unless that is a custom component you created <Modal visible={this.state.registerWithMail}>

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

Upvotes: 4

Related Questions