为了账号安全,请及时绑定邮箱和手机立即绑定

导出默认不等待我的函数返回查询。反应原生

导出默认不等待我的函数返回查询。反应原生

Helenr 2022-10-13 16:12:50
我要登录系统。我的导出默认不工作读取功能。在 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

}


查看完整回答
反对 回复 2022-10-13
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信