如何在带有组合v5的React Native中使用堆栈和抽屉

时间:2020-07-27 12:46:39

标签: react-native react-navigation react-navigation-stack react-navigation-v5 react-navigation-drawer

我是React Native的新手,需要有关navigation功能的工作原理以及如何使用Stack.screenDrawer.screen设置身份验证和仪表板屏幕的指南。以下是我的代码,我尝试编写。

请更正,并在可能的情况下进行解释。

import * as React from 'react';

import { createStackNavigator, HeaderBackButton } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import { createDrawerNavigator } from '@react-navigation/drawer';

import LoginScreen from "../layouts/LoginScreen";
import ForgotPasswordScreen from "../layouts/ForgetPassword";
import HomeScreen from '../layouts/HomeScreen';
import SettingScreen from '../layouts/SettingScreen';
import ContactUs from '../layouts/ContactUs';
import Help from '../layouts/Help';

const Stack = createStackNavigator();
const AuthFlowStack = createStackNavigator();
const Drawer = createDrawerNavigator();

const appName = "XXX";

// Took it for general all pages
const MyStack = () => {
    return (
      <NavigationContainer>
        <Stack.Navigator 
            initialRouteName='Home'
            headerMode='float'
            mode='modal'
          >
            <Stack.Screen options={{
                            headerShown: false
                          }}
                          name='Home' 
                          component={HomeScreen} />
            <Stack.Screen name="SettingScreen" component={SettingScreen} options={{headerShown:false}}/>
        </Stack.Navigator>
      </NavigationContainer>
    )
}

// Inside Drawer I want to show these pages
function MyDrawer() {
      <Drawer.Navigator initialRouteName="Home">
        <Drawer.Screen name="Home" component={MyStack} options={{headerShown:false}} />
        <Drawer.Screen name="ContactUs" component={ContactUs} options={{headerShown:false}} />
        <Drawer.Screen name="Help" component={Help} options={{headerShown:false}} />
      </Drawer.Navigator>
}

// Auth module where Login/Registration/Forgetpassword Like screen without authentication user will open
function AuthFlow() {
  <NavigationContainer>
    <AuthFlowStack.Screen name='Login' component={LoginScreen} />
    <AuthFlowStack.Screen options={({ navigation, route }) => ({
      headerLeft: ({props}) => (
        <HeaderBackButton
          {...props}
          onPress={() => {
            navigation.navigate("Login")
          }}
        />
      ),
      title: `Forgot Password? - ${appName}`,
      headerTitleAlign: 'center'
    })}
    name='ForgotPasswordScreen'
    component={ForgotPasswordScreen} />
  </NavigationContainer>
}

//Main AppStack where I'm thinking to check isLoggedIn and MyDrawer / AuthFlow I can add.
function AppStack(){
  <MyDrawer/>
}
export default {AppStack};

1 个答案:

答案 0 :(得分:0)

代码如下所示

function AuthFlow() {
    <AuthFlowStack.Navigator>
        <AuthFlowStack.Screen name='Login' component={LoginScreen} />
        <AuthFlowStack.Screen options={({ navigation, route }) => ({
            headerLeft: ({ props }) => (
                <HeaderBackButton
                    {...props}
                    onPress={() => {
                        navigation.navigate("Login")
                    }}
                />
            ),
            title: `Forgot Password? - ${appName}`,
            headerTitleAlign: 'center'
        })}
            name='ForgotPasswordScreen'
            component={ForgotPasswordScreen} />
    </AuthFlowStack.Navigator>
}

//Main AppStack where I'm thinking to check isLoggedIn and MyDrawer / AuthFlow I can add.
function AppStack() {
    <NavigationContainer>
        {
            isLoggedIn ? <MyDrawer /> : <AuthFlow />
        }
    </NavigationContainer>
}

这里有两个导航器抽屉,分别是您的应用程序堆栈和用于身份验证的堆栈导航器。

一旦更新isLoggedIn(使用上下文,状态,异步存储取决于您的应用),将基于该值显示一个导航器。

而且您在根级别上应该只有一个NavigationContainer。

最好看看https://reactnavigation.org/docs/auth-flow/来加深了解。