Android Undefined不是this.setState react native的函数
我正在开发一个react原生应用程序。当我尝试执行下面的代码时,我得到了一个错误。但同一文件的其他部分上的setStates工作正常 未定义的不是 函数(计算'this.setState({firebaseMessages:“错误 密码“})Android Undefined不是this.setState react native的函数,android,react-native,undefined,setstate,Android,React Native,Undefined,Setstate,我正在开发一个react原生应用程序。当我尝试执行下面的代码时,我得到了一个错误。但同一文件的其他部分上的setStates工作正常 未定义的不是 函数(计算'this.setState({firebaseMessages:“错误 密码“}) 您可能需要绑定回调函数,请尝试以下操作: catch(function(error) { var errorCode = error.code; var errorMessage = error.message; if (erro
您可能需要绑定回调函数,请尝试以下操作:
catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === "auth/wrong-password") {
//alert("Wrong password.");
this.setState({ firebaseMessages: "Wrong password" });
this.setState({ isModalVisibleFirebase: true });
this.setState({ loading: false })
return;
} else {
alert(errorMessage);
return;
}
console.log(error);
}.bind(this)) // change this line
或者,您可以使用ES6箭头功能,如下所示:
catch((error) => { // change this line
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === "auth/wrong-password") {
//alert("Wrong password.");
this.setState({ firebaseMessages: "Wrong password" });
this.setState({ isModalVisibleFirebase: true });
this.setState({ loading: false })
return;
} else {
alert(errorMessage);
return;
}
console.log(error);
})
如果使用ES5函数方式定义
此
范围将发生变化,请使用箭头语法来保持此
范围
所以现在您应该执行.catch((错误)=>{
而不是.catch(函数(错误){
简单的解决方法是,将
这个
分配给某个变量,然后使用该变量调用设置状态
const _this = this; //write this before fetch method
然后像这样使用
_this.setState({ firebaseMessages: "Wrong password" });
您是否在构造函数中声明了
firebaseMessages
状态?
const _this = this; //write this before fetch method
_this.setState({ firebaseMessages: "Wrong password" });