[英]React Native Firebase authentication error handling
如何在Firebase auth的错误处理函数中设置状态('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);
}
});
}
});
尝试使用es6 fat arrow语法重写函数。 在上面的代码中肯定存在的一个问题是, 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);
}
});
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.