react native - this.props.navigation loses value when called in my POST method -


inside register page have method gets called , register's user. when try navigate login page after registering user cannot read property 'navigation' of undefined. code

sendajax = () => {   //this.props.navigation returns value , can navigate '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");     })     ....   } } 

i'm not sure why this.props.navigation.navigate becomes undefined halfway down method, yet @ beginning call it. great, thanks!

this issue "this" changing you. using arrow functions keeps "outer this" in scope, instead of "this" changing because of being in new function.


Comments

Popular posts from this blog

javascript - WinJS appendTextAsync producing scheduler errors -

minify - Minimizing css files -

Sockets with kotlin -