React Navigation V5 Hide Bottom Tab in Specific Screens

风流意气都作罢 提交于 2020-06-23 14:15:07

问题


I am creating a React Native app using React Navigation version 5, and I have a bottom tab navigator with a stack navigator nested inside each screen of the tab navigator. I only want the bottom tab bar to show when on the first page of each stack navigator. Here is a snack that displays my app's basic navigation functionality: https://snack.expo.io/@brforest/hide-tab-1. Per the bottom tab documentation, there is a tabBarVisible options attribute, but:

Hiding tab bar can cause glitches and jumpy behavior. We recommend the tab navigator inside of a stack navigator instead.

The guide for nesting the tab navigator inside the stack navigator is here. I tried using this method, but I could only get it to work if I had only one stack navigator, but I need to have a stack navigator for each of the tab screens. Here is my (unsuccessful) attempt to use this method on the same app from the previous snack: https://snack.expo.io/@brforest/hide-tab-2. In this, I nested multiple tab navigators within a single stack navigator in an attempt to extrapolate the method suggested in the documentation. As you can see in this snack, the navigation within the stack does not work anymore, but the tabs still work.

To me, it makes much more sense to nest the stack navigators in the tab navigator (like I have in the first snack) than to try to nest the same tab navigator in a large stack navigators. However, I want to follow the documentation and find a way that does not cause "glitchy and jumpy behavior." Any suggestions on how I should achieve my desired navigation functionality?

Thanks!


回答1:


Like you mentioned if you only want the first screens in each stack to show the bottom tab bar then I suggest you use the second approach. Create a base stack navigator with the first screen being the tab navigator itself :

const TabScreens = ({navigation}) => { // Tab navigator with only the screens that require bottom tab bar
  return (
    <Tab.Navigator
      initialRouteName="Home"
      tabBarOptions={{
        activeTintColor: '#e91e63',
      }}>
      <Tab.Screen
        name="Home"
        component={Home}
        options={{
          tabBarLabel: 'Home',
        }}
      />
      <Tab.Screen
        name="Welcome"
        component={Welcome}
        options={{
          tabBarLabel: 'Welcome',
        }}
      />
    </Tab.Navigator>
  );
};

After creating the tab navigator, in the main renderer use:

    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          name="Stack"
          component={TabScreens}
        />
        <Stack.Screen             // Add any number of extra screens that do not require the bottom tab here
         name="Other screen 1"
         component={OtherScreen1} />
        <Stack.Screen             
         name="Other screen 2"
         component={OtherScreen2} />
      </Stack.Navigator>
    </NavigationContainer>

This way you don't have to fiddle with the bottom tab component. You can also navigate to and from any screens that part of the base stack navigator and those that are part of the Tab Navigator. The only caveat here is all the screens apart from the screens in the tab navigator will be mounted and unmounted each time you navigate there and back.




回答2:


After going through the internet I found my own way to hide the bottom tab in a specific stack screen.

export default function SignStack({ navigation, route }) {

  if (route.state?.index) {
    navigation.setOptions({
      tabBarVisible: false,
    });
  } else {
    navigation.setOptions({
      tabBarVisible: true,
    });
  }

return <Stack.Navigator> ... </Stack.Navigator>

}

This will show only bottom tab on the first stack screen.



来源:https://stackoverflow.com/questions/61831840/react-navigation-v5-hide-bottom-tab-in-specific-screens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!