【问题标题】:React Native Firebase authentication error handlingReact Native Firebase 身份验证错误处理
【发布时间】:2016-06-12 12:27:34
【问题描述】:

如何在 Firebase 身份验证的错误处理函数中设置状态 ('this.setState({})')

它在 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);
        }
    });
  }
});

【问题讨论】:

    标签: javascript reactjs firebase react-native firebase-authentication


    【解决方案1】:

    尝试使用 es6 粗箭头语法重写函数。上述代码中可以肯定的一个问题是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);
            }
        });
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-11
      • 2018-03-10
      • 2017-07-12
      • 2016-12-25
      • 2018-06-19
      • 2017-06-13
      • 2017-01-13
      • 1970-01-01
      相关资源
      最近更新 更多