如何包装每个选项卡。屏幕在同一组件中

发布于 2025-02-06 06:57:20 字数 1970 浏览 3 评论 0原文

当使用React-Navigation的bottom tabnavigator时,如何将每个屏幕用组件包装?例如,说我有一些类似的选项卡:

<NavigationContainer>
      <Tab.Navigator tabBar={(props) => <MyTabBar {...props} />}>
          <Tab.Screen name="Home" component={HomeScreen} />
          <Tab.Screen name="Settings" component={SettingsScreen} />
      </Tab.Navigator>
</NavigationContainer>

我想用相同的组件包装这两个屏幕,但是我需要塔巴尔得到的相同道具,因为我想根据活动路线在包装组件中渲染一些不同的东西。 我可以在每个屏幕的渲染中手动添加组件,但是我想将其放在一个地方,并动态弄清楚渲染内容。

仅供参考,在React-Navigation v4中,我可以这样做:

// CustomTabView component

const { routes, index } = navigation.state;
const route = routes[index];
const descriptor = descriptors[route.key];
const ActiveScreen = descriptor.getComponent();
const currentKey = descriptor.key;

<View style={Styles.container}>
    <ShellHeader
        icon={route.params.actionIcon}
        currentRoute={route}
        currentTab={currentKey}
    />
    <ActiveScreen navigation={descriptor.navigation} />
    <CustomTabBar navigation={navigation} />
</View>

const AppContainer = createAppContainer(createNavigator(CustomTabView, CustomTabRouter, {}));

fastortabrouter看起来像:

const CustomTabRouter = TabRouter(
    {
        Home: {
            screen: HomeScreen,
            params: {
                key: 'history',
                actionIcon: {
                    navigation: () => NavigationService.navigateToHomeScreen()
                }
            }
        },
        Settings: {
            screen: SettingsScreen,
            params: {
                key: 'contacts',
                actionIcon: {
                    navigation: () => NavigationService.navigateToSettingsScreen()
                }
            }
        }
    {
        initialRouteName: 'Home'
    }
);

但是在v6中不起作用,因为没有create> createEappContainer

when using react-navigation's BottomTabNavigator, how can I wrap each screen with a component? For example, say I have some tabs like so:

<NavigationContainer>
      <Tab.Navigator tabBar={(props) => <MyTabBar {...props} />}>
          <Tab.Screen name="Home" component={HomeScreen} />
          <Tab.Screen name="Settings" component={SettingsScreen} />
      </Tab.Navigator>
</NavigationContainer>

I want to wrap those two screens with the same component, BUT I need the same props that tabBar is getting because I want to render something different in the wrapped component based on the active route.
I could manually add the component in each screen's render, but I'd like to keep it in just one place and have it dynamically figure out what to render.

Just for reference, in react-navigation v4 I could do it like this:

// CustomTabView component

const { routes, index } = navigation.state;
const route = routes[index];
const descriptor = descriptors[route.key];
const ActiveScreen = descriptor.getComponent();
const currentKey = descriptor.key;

<View style={Styles.container}>
    <ShellHeader
        icon={route.params.actionIcon}
        currentRoute={route}
        currentTab={currentKey}
    />
    <ActiveScreen navigation={descriptor.navigation} />
    <CustomTabBar navigation={navigation} />
</View>

const AppContainer = createAppContainer(createNavigator(CustomTabView, CustomTabRouter, {}));

and CustomTabRouter would look something like:

const CustomTabRouter = TabRouter(
    {
        Home: {
            screen: HomeScreen,
            params: {
                key: 'history',
                actionIcon: {
                    navigation: () => NavigationService.navigateToHomeScreen()
                }
            }
        },
        Settings: {
            screen: SettingsScreen,
            params: {
                key: 'contacts',
                actionIcon: {
                    navigation: () => NavigationService.navigateToSettingsScreen()
                }
            }
        }
    {
        initialRouteName: 'Home'
    }
);

But that does not work in v6 as there is no createAppContainer anymore

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文