React原生Firebase身份验证错误处理
问题描述:
如何在Firebase身份验证的内部错误处理函数中设置状态('this.setState({})')
?React原生Firebase身份验证错误处理
它在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胖箭头语法重写函数。上述代码中肯定存在的一个问题是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);
}
});
}
})
感谢它的工作吧! :d –