signedIn函数返回一个promise。如果您正在调用下面这样的函数,然后每次我们都会导航到SCREN1。
我想显示基于AsyncStorage的正确导航器,我在这里搜索了一下,但总是得到相同的结果:
const signedIn = async () => {
const token = await AsyncStorage.getItem('token');
if(token) {
return true;
} else {
return false;
}
}
它总是返回true。。
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
}