React-Native 中的屏幕导航

huangapple go评论49阅读模式
英文:

Screens Navigation in React-Native

问题

我理解你的需求。你需要在用户打开应用时,根据他们是否已登录来显示不同的界面。以下是一种实现方式:

  1. 使用AsyncStorage来保存用户的登录状态。当用户成功登录时,你可以将一个标志位存储在AsyncStorage中,表示用户已登录。

  2. 在你的应用的入口处,检查AsyncStorage中的标志位。如果标志位存在且表示用户已登录,那么导航到底部标签页(MyTabs),否则导航到授权页面(AuthorizationScreen)。

  3. 当用户点击注销按钮时,你可以再次更新AsyncStorage中的标志位,表示用户已注销,然后导航到授权页面。

这样,你可以根据用户的登录状态在应用的不同部分显示不同的界面。希望这能帮助你实现你的需求。

英文:

I have the following structure of screens and I am not sure how to do them. I have a bottom tab screen, where each bottom tab is stack of screens by itself. but before coming to the bottom tab screen, I want to authorize the user. so I have another stack of screens to deal with authentication. How do I make this possible(i.e. when a user starts the app, if that user is already logged in, then they will be presented with the bottom tabs page but if they haven't signed in, then they will be presented with signup/signin page).
I have included the following for more context:
Authentication page

const authentication = createNativeStackNavigator();
const AuthorizationScreen = ({navigation}) => {
    const {bgcolr}  = React.useContext(ThemeContexts)
  return (
    <>
    <authentication.Navigator initialRouteName="Welcome" screenOptions={{tabBarVisible: true,headerStyle:{backgroundColor:bgcolr}}} >
        <Authorization.Screen name='Signup' component={Signup} />
        <Authorization.Screen name='Signin' component={Signin} />
    </authentication.Navigator>
    </>
  )
}

Bottom tabs page

const MyTabs = ({navigation})=>{
    const Tab = createBottomTabNavigator();
    return (
        
        <Tab.Navigator>
            <Tab.Screen name="Home" component={HomeStackScreen} />
            <Tab.Screen name="Profile" component={ProfileStackScreen}/> 
        </Tab.Navigator>
    )
}

and in home tab of the bottom tab the following stack of screens exist

const ProfileStack = createNativeStackNavigator();
const ProfileStackScreen = () => {
  return (
    <>
    <ProfileStack.Navigator initialRouteName = "Profiles">
        <ProfileStack.Screen name ="Profiles" component={Profile}/>
        <ProfileStack.Screen name="settings" component={Settings}/>
        <ProfileStack.Screen name="Logout" component={Logout} />
    </ProfileStack.Navigator>
    </>
  )
}

so I want to display a user with a authorization screen if they haven't already signed in. but show them MyTabs bottom tab if they have already Signin when they first opened the app. How do I do that. Also, once the user has signed in, if the logout, they are presented with the authorization screen again. I know I need to use, Asyncstorage to save authorization properties. but I don't know how to proceed beyond that.

答案1

得分: 1

你好。我强烈推荐查看 React Native Navigation 的官方网站。它有很好的文档,解释了如何根据用户的登录状态显示屏幕:如果已登录,它将显示特定的屏幕,如果未登录,它将显示认证屏幕。我在工作中遵循了它们的指导,使用 Redux Toolkit 作为全局状态管理,这种架构绝对令人惊叹。祝好运。
https://reactnavigation.org/docs/auth-flow/

英文:

Good day. I highly recommend checking this official website of react native navigation. it's well-documented and explains how you show screens based on what the user signs in: it will show specific screens, and if not, it will show the authentication screen. I followed them at work, using the redux toolkit as global state management, and this architecture's absolutely amazing. good luck.
https://reactnavigation.org/docs/auth-flow/

答案2

得分: 0

要使用堆栈导航,可以像这样使用

<NavigationContainer ref={navigationRef}>
      <Stack.Navigator
        screenOptions={{
          headerShown: false,
        }}
        initialRouteName={YourInitialScreenName}>
            <Stack.Screen name={YourInitialScreenName} component={YourInitialScreen}/>
            <Stack.Screen name={Routes.Dashboard} component={CustomTabComponent} />
      </Stack.Navigator>
</NavigationContainer>

对于选项卡导航,您需要编写如下代码:

<Tab.Navigator
            screenOptions={{
              headerShown: false,
            }}
            initialRouteName={Routes.Home}
            tabBar={props => <CustomTabBar {...props} />}>
      <Tab.Screen name={Routes.Home} component={Home}/>
</Tab.Navigator>

有关堆栈导航的更多详细信息,请查看此链接:
https://reactnavigation.org/docs/stack-navigator

有关选项卡导航的详细信息,请查看下面的链接:
https://reactnavigation.org/docs/bottom-tab-navigator

英文:

To use Stack navigation, You can use like this

&lt;NavigationContainer ref={navigationRef}&gt;
      &lt;Stack.Navigator
        screenOptions={{
          headerShown: false,
        }}
        initialRouteName={YourInitialScreenName}&gt;
            &lt;Stack.Screen name={YourInitialScreenName} component={YourInitialScreen}/&gt;
            &lt;Stack.Screen name={Routes.Dashboard} component={CustomTabComponent} /&gt;
      &lt;/Stack.Navigator&gt;
&lt;/NavigationContainer&gt;

For tab navigation you have to do code like this:

&lt;Tab.Navigator
            screenOptions={{
              headerShown: false,
            }}
            initialRouteName={Routes.Home}
            tabBar={props =&gt; &lt;CustomTabBar {...props} /&gt;}&gt;
      &lt;Tab.Screen name={Routes.Home} component={Home}/&gt;
&lt;/Tab.Navigator&gt;

For more details of stack navigation, you can check this link:-
https://reactnavigation.org/docs/stack-navigator

For tab navigation details check below link
https://reactnavigation.org/docs/bottom-tab-navigator

huangapple
  • 本文由 发表于 2023年5月25日 03:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定