1

How to set state ('this.setState({})') inside error handling function for Firebase auth?

its not working in React Native.

  onSignUpPress() {
    if (this.state.password !== this.state.passwordConfirmation ) {
      return this.setState({errorMessage: 'Your passwords do not match'});
    }

      ref.createUser({
        email    : this.state.email,
        password : this.state.password
      }, function(error, authData) {
        if (error) {
            console.log(error);
            // this.setState({errorMsg: error}) <-- Like this, it not work on React Native.
        } else {
            console.log("Successfully created user account with uid:", userData.uid);
        }
    });
  }
});

1 Answer 1

3

Try rewriting the function using es6 fat arrow syntax. One issue for sure in the above code is that this is not bound to the correct scope. Try writing the function like this:

onSignUpPress() {
    if (this.state.password !== this.state.passwordConfirmation ) {
      return this.setState({errorMessage: 'Your passwords do not match'});
    }

      ref.createUser({
        email    : this.state.email,
        password : this.state.password
      },(error, authData) => {
        if (error) {
            console.log(error);
            this.setState({errorMsg: error})
        } else {
            console.log("Successfully created user account with uid:", userData.uid);
        }
    });
  }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.