React Native 子组件中的 userId - AsyncStorage 实现?

问题描述

我在 App.js 中有一个 signIn 布尔值,它为身份验证流进行条件渲染:

    {signedIn===false ?
    <Stack.Screen name="authenticationFlow" component={authenticationFlow} options={{ title: 'Sign Up' }} />
    : signedIn===true && signUpCompleted===true ?
    <Stack.Screen name="mainFlow" component={mainFlow} options={{ title: 'Main Flow' }} />
    : signedIn===true && signUpCompleted===false ?
    <Stack.Screen name="userDetails" component={userDetailsFlow} options={{ title: 'User Details' }} />
    : null }

我还在一个状态变量中包含 userId 和 userInfo,该变量通过 Context.Provider 传递给需要它的子组件。

但是,我现在在注销时遇到问题,其中批处理了以下两个状态调用

    setSignedIn(false)
    setUserId('')

屏幕中仍然会有一些渲染,因为它仍然处于活动状态并且使用了导致错误的 userId = ''。

所以现在我想我必须使用 AsyncStorage 来存储 userId 以便在子组件更改时不会触发重新渲染。但是如何在子组件中询问 userId 呢?如果我做例如子组件中的以下内容——在每次渲染时都要求它——我认为这会导致我的应用程序因为等待而变慢?

const [exampleState,setExampleState] = useState()
const userId = await AsyncStorage.getItem('@userId')

我该怎么做?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)