如何在 React Native 6.x 版中使用多个导航器

马丁·布佐兹

我刚开始反应原生。我试图在我的应用程序中使用多个导航 - 底部标签导航和抽屉导航。我已成功添加底部选项卡导航,但是当我尝试添加抽屉导航时出现错误:

“另一个导航器已为此容器注册。您可能在单个“导航容器”或“屏幕”下有多个导航器。确保每个导航器都位于单独的“屏幕”容器下。“

我想在我的应用程序中使用两个导航器。

这是我的代码:

底部Tabs.js:

import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import React from "react";

import about from "../screens/about";
import home from "../screens/home";
import reviewDetails from "../screens/reviewDetails";

const Tab = createBottomTabNavigator();

const AppNavigator = () => (
  <Tab.Navigator>
    <Tab.Screen
      name="about"
      component={about}
      options={{
        title: "About",
      }}
    ></Tab.Screen>
    <Tab.Screen
      name="home"
      component={home}
      options={{
        title: "Home",
      }}
    ></Tab.Screen>
    <Tab.Screen name="reviewDetails" component={reviewDetails}></Tab.Screen>
  </Tab.Navigator>
);

export default AppNavigator;

抽屉导航.js

import React from "react";
import { createDrawerNavigator } from "@react-navigation/drawer";
import { NavigationContainer } from "@react-navigation/native";
import "react-native-gesture-handler";

import home from "../screens/about";
import about from "../screens/home";

const Drawer = createDrawerNavigator();

export default function App() {
  return (
    <Drawer.Navigator initialRouteName="Home">
      <Drawer.Screen name="Home" component={home} />
      <Drawer.Screen name="About" component={about} />
    </Drawer.Navigator>
  );
}

应用程序.js

import React from "react";
import { NavigationContainer, useNavigation } from "@react-navigation/native";
import BottomTabs from "./navigation/bottomTabs";
import DrawerNavigator from "./navigation/drawerNavigarion";

export default function App() {
  return (
    <>
      <NavigationContainer>
        <BottomTabs></BottomTabs>
        <DrawerNavigator></DrawerNavigator>
      </NavigationContainer>
    </>
  );
}

这是我想要的: 导航

如果我想在图片示例中使用两者,我应该在代码中更改什么?

FnH

为了使用多个导航,您需要使用nesting.

参考: https : //reactnavigation.org/docs/nesting-navigators/

例如: GitHub

修改:

应用程序.js

    <NavigationContainer>
      <DrawerNavigator />
    </NavigationContainer>

抽屉导航.js

    import BottomTabs from "./navigation/bottomTabs";
    //...

    <Drawer.Navigator initialRouteName="Tab">
      <Drawer.Screen name="Tab" component={BottomTabs} />
    </Drawer.Navigator>

底部标签.js

    <Tab.Navigator initialRouteName="Home">
      <Tab.Screen name="About" component={About} />
      <Tab.Screen name="Home" component={Home} />
      <Tab.Screen name="ReviewDetails" component={ReviewDetails} />
    </Tab.Navigator>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 React Native 中使用单个导航器而不是多个导航器?

如何在底部标签导航器中隐藏标签栏,React Native 6x

如何在React Native中使用抽屉导航器?

如何在 React Native 导航 v6 中的 React Native 类组件中使用 context 和 useTheme?

如何在React Native v.5(不是v.4)中嵌套多个导航器

如何在React Navigation中使用NavigationService重置导航器?

如何在React Native导航器中禁用向后滑动

如何在React Native导航器中传递参数?

如何在React Native中实现Drawer导航器?

如何在 React Native 中不使用堆栈导航器重定向到页面?

如何在 React Native 导航中使用 modalPresentationStyle .fullscreen

如何在 React Native 中使用组件导航

如何在React Native中使用OpenCV

如何在 React Native 中使用容器?

如何在React Native中使用Elasticsearch?

如何在Typescript中使用React Native?

如何在React Native中使用Iframe?

如何在React Native中使用componentWillUnmount?

如何在 React Native 中使用 SVG?

如何在 React Native 中使用 React Native Video 显示多个视频?

如何在 React Native 的屏幕之间导航?

如何在React Native中从自定义导航器导航到createBottomTabNavigator?

在React Native中获取后如何更改屏幕?[使用堆栈导航器]

如何在React Native中使用React.forwardRef()

如何在React / React Native中使用Emscripten编译的JavaScript

react native - 如何在元组数组中使用 React Hooks?

如何在 React Native 中自定义材质底部选项卡导航器?

如何在React Native中将组件正确导入到我的导航器中?

如何在React Native导航器中禁用向后滑动手势