如何包装每个选项卡。屏幕在同一组件中
当使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论