我正在使用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很陌生,所以请解释一下。
最佳答案
你不能像这样多次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/