reactjs - React-Native 错误 "undefined is not an object (evaluating ' _this2.onLoginSuccess.bind')"

标签 reactjs react-native react-native-ios

我正在使用react-native中的一个简单示例来登录用户。我添加了以下代码来处理该问题

onButtonPress() {
    const { email, password } = this.state;
    this.setState({ error: '', loading: true });

    firebase.auth().signInWithEmailAndPassword(email, password)
    .then(this.onLoginSuccess.bind(this))
    .catch(() => {
      firebase.auth().createUserWithEmailAndPassword(email, password)
      .then(this.onLoginSucess.bind(this))
      .catch(this.onLoginFail.bind(this));
    });
}

onLoginSuccess() {
    this.setState({
      email: '',
      password: '',
      loading: false,
      error: ''
    });
}

onLoginFail() {
    this.setState({
      error: 'Authentication Failed',
      loading: false
    });
}

但我收到错误“未定义不是对象(评估'_this2.onLoginSuccess.bind')

我对react-native很陌生,所以请解释一下。

enter image description here

最佳答案

你不能像这样多次bind()bind() 就位通常且仅适用于匿名函数。

这样做:

constructor(props) {
    super(props);

    this.onLoginSuccess = this.onLoginSuccess.bind(this);
    this.onLoginFailed = this.onLoginFailed.bind(this);
} 

onButtonPress() {
    const { email, password } = this.state;
    this.setState({ error: '', loading: true });

    firebase.auth().signInWithEmailAndPassword(email, password)
    .then(this.onLoginSuccess)
    .catch(() => {
      firebase.auth().createUserWithEmailAndPassword(email, password)
      .then(this.onLoginSucess)
      .catch(this.onLoginFail);
    });
}

onLoginSuccess() {
    this.setState({
      email: '',
      password: '',
      loading: false,
      error: ''
    });
}

onLoginFail() {
    this.setState({
      error: 'Authentication Failed',
      loading: false
    });
}

关于reactjs - React-Native 错误 "undefined is not an object (evaluating ' _this2.onLoginSuccess.bind')",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47898174/

相关文章:

reactjs - React Native 中 Java 单例的等价物是什么?

reactjs - Jest 模拟模块解析变量值

javascript - 如何在使用 React-Navigation 和 Expo 的同时实现一次字体?

javascript - react native 捆绑无法工作(已完成但未捆绑项目)

google-maps - React Native,不变违规 : Native component for "AIRMap" does not exist

react-native - 如何在 ubuntu 19.04 中完全卸载 brew 并重新安装 brew

javascript - react 日期选择器。如何节省状态时间?

javascript - 如何通过react router传递和使用查询参数?

javascript - 如何从 antd Image 中删除编辑器图标

ios - 捆绑 index.ios.js 时内置卡在 0.0%