我要登录系统。我的导出默认不工作读取功能。在 AsyncStorage 中读取函数查询 user_id。请帮我 :)应用程序.jsvar sendTabs = <TabsScreens />var sendLogin = <LoginScreens />var Read = readStore = async () => { try { const value = await AsyncStorage.getItem('user_id'); if (value !== null) { return 1; } else { return 0; } } catch (e) { alert(e); }}var Welcome;Read().then((response) => { if (response == 1) { Welcome = sendTabs } else { Welcome = sendLogin; }});export default () => (Welcome)
1 回答
沧海一幻觉
TA贡献1824条经验 获得超5个赞
您可以定义新组件来处理此逻辑:
export function WelcomeScreen() {
const [isLoggedIn, setIsLoggedIn] = React.useState(null); // null | true | false
React.useEffect(() => {
void async function getUserId() {
const id = await AsyncStorage.getItem('user_id');
setIsLoggedIn(Boolean(id)); // true -> user_id found, false -> no valid user_id found
}();
}, []);
return (userId === null)
? <Text>Loading...</Text> // this will show until storage is checked
: (isLoggedIn) ? <TabsScreens /> : <LoginScreens />; // depending on the value of id from storage you show tabs or login screen
}
添加回答
举报
0/150
提交
取消