简体   繁体   English

React Native - 如何在每个屏幕中保留 TabNavigator

[英]React native - how to keep TabNavigator in every screen

I am using a combination of tab navigator, stack navigator, and switch nagivator in my react native app with react-navigation .我在带有react-navigation本机应用程序中使用选项卡导航器、堆栈导航器和切换react-navigation

This works well, except I'd like to keep the bottom tabs in every screen.这很有效,除了我想在每个屏幕中保留底部标签。 Currently if I navigate to UserProfile screen, then the bottom tabs go away.目前,如果我导航到UserProfile屏幕,那么底部的选项卡就会消失。

App.js:应用程序.js:

const TabStack = createBottomTabNavigator({
  Feed: { screen: FeedScreenStack },
  MyProfile: { screen: ProfileScreenStack },
});

const UserStack = createStackNavigator(
  { 
    UserProfile: { screen: userProfile },
    Comments: { screen: comments }
  }
);

const MainStack = createSwitchNavigator(
  {
    Home: TabStack,
    User: UserStack,
    Auth: AuthStack
  },
  {
    initialRouteName: 'Home'
  }
);

To solve this, I tried making a component out of the tab navigator and just importing it in userProfile.js , like <BottomTab /> .为了解决这个问题,我尝试从选项卡导航器中创建一个组件,然后将其导入userProfile.js ,例如<BottomTab />

bottomTab.js:底部Tab.js:

const BottomTab = createBottomTabNavigator({
    Feed: { screen: FeedScreenStack },
    MyProfile: { screen: ProfileScreenStack },
})

export default BottomTab

but this doesn't work because in react-navigation 3 I have to set up the app container directly and I am not sure how to get around it.但这不起作用,因为在 react-navigation 3 中,我必须直接设置应用程序容器,但我不确定如何绕过它。 The only other solution I can think of is to create a custom component that is just a bar with buttons pointing to different pages, but I wonder if I can just use TabNavigator?我能想到的唯一其他解决方案是创建一个自定义组件,它只是一个带有指向不同页面的按钮的栏,但我想知道我是否可以只使用 TabNavigator?

The recommended way is that a TabNavigation is the Top Item in your navigation stack.推荐的方法是 TabNavigation 是导航堆栈中的顶部项目。 In each of the tabs you might have a different StackNavigation.在每个选项卡中,您可能有不同的 StackNavigation。 If you use the SwitchNavigation for Auth which probably means a Login-Screen, you might put the SwitchNavigation at top with the two children Auth and Tab and include Home, User, Feed, etc. in different StackNavigations that are children of the TabNavigation.如果您使用 SwitchNavigation 进行身份验证,这可能意味着登录屏幕,您可以将 SwitchNavigation 与两个子项 Auth 和 Tab 放在顶部,并在作为 TabNavigation 子项的不同 StackNavigations 中包含 Home、User、Feed 等。 You could change for example as following:例如,您可以更改如下:

const TabStack = createBottomTabNavigator({
  Feed: { screen: FeedScreenStack },
  User: { screen: UserStack },
});

const UserStack = createStackNavigator({ 
  UserProfile: { screen: userProfile },
  Comments: { screen: comments },
  MyProfile: { screen: ProfileScreenStack },
});

const MainStack = createSwitchNavigator({
  Home: TabStack,
  Auth: AuthStack
},
{
  initialRouteName: 'Home'
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM