gpt4 book ai didi

javascript - React Native Firebase 身份验证错误处理

转载 作者:行者123 更新时间:2023-11-30 12:04:14 25 4
gpt4 key购买 nike

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

最佳答案

尝试使用 es6 粗箭头语法重写该函数。上面代码中肯定存在的一个问题是 this 没有绑定(bind)到正确的范围。尝试像这样编写函数:

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

关于javascript - React Native Firebase 身份验证错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com