0

i cant find my mistake why should i use cleanup , and return anything when i am use useefect i just want to check if the user is logged in or not .. ?

useEffect(async () => {
    var item = await AsyncStorage.getItem("User");
    console.log(item);
    if (item == null || item == undefined) {
      props.navigation.navigate("Login");
    }
    else {
      var user = await JSON.parse(item);
      if(user.fullname=="admin"){
        props.navigation.navigate("AdminHP");
      }
      else{
        props.navigation.navigate("UserHP");
      }
    }
    console.log("effect");
    
  },[]);
skyboyer
  • 19,620
  • 7
  • 50
  • 62
  • You need to use try-catch, may be there is any error throw. – Mohinder singh Aug 05 '21 at 10:59
  • 1
    Does this answer your question? [React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing](https://stackoverflow.com/questions/53332321/react-hook-warnings-for-async-function-in-useeffect-useeffect-function-must-ret) – lusc Aug 05 '21 at 10:59
  • `useEffect(async () =>...` returns a promise. Move the code to a separat function and just call it in `useEffect( () => { checkAndRedirect() })` – Martin Aug 05 '21 at 11:00

1 Answers1

1
useEffect(() => {
    async function check() {
      var item = await AsyncStorage.getItem("User");
    console.log(item);
    if (item == null || item == undefined) {
      props.navigation.navigate("Login");
    }
    else {
      var user = await JSON.parse(item);
      if(user.fullname=="admin"){
        props.navigation.navigate("AdminHP");
      }
      else{
        props.navigation.navigate("UserHP");
      }
    }
    console.log("effect");
    }

    check()
  }, [])