0

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

1 回答 1

1

您只需在组件挂载时调用 isUserLogged ,然后只需检查状态: 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);
     }
});

应用程序.js

componentDidMount(){
    ParseManager.isUserLogged(
       (logged)=>{
         this.setState({logged});});
       });
}

render() {
  if (this.state.logged) {
    return (
      <SecondaryRoot />
    );
  } else {
    return (
      <MainRoot />
    );
  }
于 2018-10-30T12:39:45.007 回答