检查AsyncStorage中的现有密钥

时间:2018-07-14 09:52:21

标签: react-native navigation token asyncstorage

我想显示基于AsyncStorage的正确导航器,我在这里搜索但总是相同的结果:

 const signedIn = async () => {
    const token = await AsyncStorage.getItem('token');
    if(token) {
      return true;
    } else {
      return false;
    }
  }

它总是返回true。.

1 个答案:

答案 0 :(得分:1)

signedIn函数返回一个诺言。如果您要调用以下功能, 然后每次我们导航至 SCREEN1

let response = signedIn();
if(response){
   console.log("SCREEN1")
   //Navigate to SCREEN 1
 }else{
   console.log("SCREEN2")
   // Navigate to SCREEN 2
}

而不是您应该这样称呼

signedIn().then((response)=>{
  if(response){
     console.log("SCREEN1")
     //Navigate to SCREEN 1
   }else{
     console.log("SCREEN2")
     // Navigate to SCREEN 2
  }
 })

或者通过这种方式{调用函数应该是异步的

let response = await signedIn()
if(response){
   console.log("SCREEN1")
   //Navigate to SCREEN 1
}else{
   console.log("SCREEN2")
   // Navigate to SCREEN 2
}