简体   繁体   English

React native:如何让多个抽屉导航器链接指向同一个堆栈导航器中的屏幕

[英]React native: How can I have multiple drawer navigator links point to screens within the same stack navigator

I am new to react native and I haven't seen this question asked by anyone or haven't found a way around this.我是本地人的新手,我还没有看到任何人问过这个问题,或者没有找到解决这个问题的方法。

Using react navigation 5 with expo.使用带有 expo 的反应导航 5。

Currently I have a the following app structure: Stack navigator inside of drawer navigator.目前我有以下应用程序结构:抽屉导航器内的堆栈导航器。

Example of page structure:
Drawer Navigator ( links ):
Home (RouteStack)
Screen 1 
Screen 2
Screen 3

RouteStack( screens) :
Home ( initial route )
Screen 1
Screen 2
Screen 4

How can I get Screen 1/Screen 2 link in drawer navigator load RouteStack: Screen 1/Screen 2?如何在抽屉导航器加载 RouteStack:屏幕 1/屏幕 2 中获取屏幕 1/屏幕 2 链接? These links are provided to easily jump to the required screen.提供这些链接是为了轻松跳转到所需的屏幕。

Need some guidance on how to achieve this.需要一些关于如何实现这一目标的指导。

I have thought of the possibility of drawer inside of stack, but there are screens inside of drawer that may not be listed in the stack.我已经考虑过抽屉内的堆栈的可能性,但抽屉内的屏幕可能未在堆栈中列出。 Hence, went with stack inside of drawer.因此,与抽屉内的堆栈一起去。

I have also tried to do a navigation.navigate(route.name) inside of RouteStack我也尝试在 RouteStack 内做一个 navigation.navigate(route.name)

Sample code: Drawer navigator:示例代码:抽屉导航器:

<NavigationContainer>
      <Drawer.Navigator drawerContent={(props, navigation) => <CustomDrawerContent {...props} {...navigation} />}>
        <Drawer.Screen name="Home" component={RouteStack} />
        <Drawer.Screen name="MyItems" component={RouteStack} />
        <Drawer.Screen name="ContactRep" component={RouteStack} />
        <Drawer.Screen name="Settings" component={SettingInfo} />
      </Drawer.Navigator>
    </NavigationContainer>

Stack navigator (RouteStack) looks like this:堆栈导航器 (RouteStack) 如下所示:

   <Stack.Navigator
      initialRouteName="Home"
      screenOptions={{ gestureEnabled: false, headerTitleAlign: 'auto' }}
    // headerMode="float"
    >
      <Stack.Screen
        name="Home"
        component={HomeScreen}
        options={{
          title: '',
          headerStyle: {
            backgroundColor: '#fff',
          },
          headerTintColor: '#000',
          headerTitleStyle: {
            fontWeight: 'bold'
          },
          headerLeft: props => <HeaderLeftMenu {...props} />,
          headerRight: props => <HeaderRightMenu {...props} />,
          headerTitle: props => <HeaderTitle {...props} />
        }}
      />
      <Stack.Screen
        name="ContactRep"
        component={ContactRep}
        options={{ headerTitle: props => <HeaderTitle {...props} /> }}
      />
      <Stack.Screen
        name="MyItems"
        component={MyItems}
        options={{ headerTitle: (props, navigation) => <HeaderTitle {...props} /> }}
           />

    </Stack.Navigator>

Thanks in advance and help is appreciated.在此先感谢您的帮助。

Your method is fine.你的方法很好。 But to clarify your ideas I will give you an example.但是为了澄清你的想法,我会给你一个例子。

Assume I have a main Drawer.假设我有一个主抽屉。 In that drawer I can navigate to 2 different screens.在那个抽屉里,我可以导航到 2 个不同的屏幕。 Inside those screens, I can navigate and do diferent things (like going to sub-screens), but never go outside the drawer.在这些屏幕内,我可以导航和做不同的事情(比如转到子屏幕),但不能在抽屉外做 go。

To do this, we would have to created nested navigators .为此,我们必须创建嵌套的导航器。 This meaning, one type of navigator if going to be inside another one.这意味着,如果要进入另一种导航器,则一种导航器。 In our case of example:在我们的例子中:

<Papa Drawer>
 <Screen 1 component={StackSon1}>
 <Screen 2 component={StackSon2}>
<Papa Drawer>

And then StackSon1, for example, will look like this:例如,StackSon1 将如下所示:

StackSon = () => {
return (
<Stack.Navigator>
 <Stack.Screen>
 <Stack.Screen>
 ...
  )
}

React Navigation will also handle every drawer separately, meaning that you don't have to worry about the user creating an infinite chain of open screens. React Navigation 还将单独处理每个抽屉,这意味着您不必担心用户创建无限的打开屏幕链。

Also, remember that, when we Nest navigators using a function (like I did) we must use return (or the simplified version of return with just parenthesis)另外,请记住,当我们使用 function 嵌套导航器时(就像我一样),我们必须使用return (或仅带括号的 return 的简化版本)

Hope it helps.希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在特定屏幕上禁用反应本机堆栈导航器? - How can I disable react native stack navigator on specific screens? 我如何向在本机中具有堆栈导航的抽屉式导航器项目添加图标 - how can i add icons to drawer navigator items that have stack navigation in react native 如何将多个导航器相互嵌套? 即在抽屉导航器中嵌套底部选项卡并将抽屉导航器嵌套到堆栈导航器 - How to nest multiple navigator within each other? i.e. nesting bottom tab in drawer navigator and nesting drawer navigator to stack navigator 在本机反应中使用抽屉导航器和堆栈导航器 - using drawer navigator and stack navigator in react native React Native 中的嵌套堆栈导航器和抽屉导航器 - Nested Stack Navigator and Drawer Navigator in React Native 在 react native 中将堆栈导航器与抽屉导航器结合使用 - Combining a stack navigator with a drawer navigator in react native 在React Native中如何在堆栈导航器和抽屉式导航器之间传递数据 - How to pass data between stack navigator and drawer navigator in react native 如何在 react-native 中结合使用 Drawer Navigator 和 Stack Navigator? - How to use Drawer Navigator & Stack Navigator combined in react-native? React Native-Tab Navigator内Drawer Navigator内的Stack Navigator - React Native - Stack Navigator inside Drawer Navigator inside Tab Navigator 在所有屏幕上显示本机抽屉导航器 - react native drawer navigator show on all screens
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM