Search code examples
javascriptreact-nativeparse-platform

undefined is not a function (evaluating this.function() in react native


So what I have is this: I want to keep all the 'parse' code inside a manager that I can call from another classes and files. On this example I have a function that will only check if the user is logged in and then return different drawer navigations based on that. The problem is that I keep getting the error 'undefined is not a function (evaluating ...)'. Im kinda new to javascript and couldn't find an answer to this. Here's the code.

Thanks in advance.

ParseManager.js

isUserLogged() {
Parse.User.currentAsync().then(function (user) {
    if (user) {
        console.log("ParseManager - User logged in.")
        return true;
    } else {
        console.log("ParseManager - User logged off.")
        return false;
    }
});

App.js

render() {
if (ParseManager.isUserLogged()) {
  return (
    <SecondaryRoot />
  );
} else {
  return (
    <MainRoot />
  );
}

Solution

  • You simply call isUserLogged on component mount and later simply check the state: ParseManager.js

    isUserLogged(callback) {
       Parse.User.currentAsync().then(function (user) {
         if (user) {
             console.log("ParseManager - User logged in.")
             callback(true);
         } else {
             console.log("ParseManager - User logged off.")
             callback(false);
         }
    });
    

    App.js

    componentDidMount(){
        ParseManager.isUserLogged(
           (logged)=>{
             this.setState({logged});});
           });
    }
    
    render() {
      if (this.state.logged) {
        return (
          <SecondaryRoot />
        );
      } else {
        return (
          <MainRoot />
        );
      }