React native 在my POST方法中调用this.props.navigation时会失去值
在我的register页面中,我有一个被调用的方法,register是一个用户。但是,在注册用户后尝试导航回登录页面时,我无法读取未定义的属性“navigation”。我的代码是React native 在my POST方法中调用this.props.navigation时会失去值,react-native,react-navigation,react-native-ios,React Native,React Navigation,React Native Ios,在我的register页面中,我有一个被调用的方法,register是一个用户。但是,在注册用户后尝试导航回登录页面时,我无法读取未定义的属性“navigation”。我的代码是 sendAjax = () => { //this.props.navigation returns a value and I can navigate to 'Login' if(!Regex.test(this.state.email)){ }else if(this.state.passwo
sendAjax = () => {
//this.props.navigation returns a value and I can navigate to 'Login'
if(!Regex.test(this.state.email)){
}else if(this.state.password != this.state.confirmPwd){
Alert.alert("The passwords don't match!");
}else{
const fn = encodeURIComponent(this.state.firstname);
...
const hashDigest = sha256(p);
const requestBody = `firstname=${fn}...
//POST
fetch("http://localhost:3000/users", {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: requestBody
}).then(function (res, next) {
console.log("fetch request ", JSON.stringify(res.ok));
if(res.ok){
res.json().then(function (json) {
if(json.registerstate){
Alert.alert('Register success, please login');
//Cannot read property 'navigation' of undefined
this.props.navigation.navigate('Login')
}
});
}
})
.then((res) => {
Alert.alert("then working");
})
....
}
}
我不知道为什么这个.props.navigation.navigate在方法的中途变得未定义,但在一开始我可以调用它。任何帮助都会很好,谢谢 这个问题是“这个”正在为你改变。使用箭头函数将“外部此”保持在范围内,而不是“此”因处于新函数中而改变。不要使用常规函数!使用箭头功能!嘿@AndrewLi我想我使用的是一个箭头函数(sendAjax=()=>{})不:
。然后(函数(res,next){
,和。然后(函数(json){
啊,好的,我不知道你需要-非常感谢!