Hiding tab bar in specific screens
Sometimes we may want to hide the tab bar in specific screens in a stack navigator nested in a tab navigator. Let's say we have 5 screens: Home
, Feed
, Notifications
, Profile
and Settings
, and your navigation structure looks like this:
- Static
- Dynamic
const HomeStack = createNativeStackNavigator({
screens: {
Home: Home,
Profile: Profile,
Settings: Settings,
},
});
const MyTabs = createBottomTabNavigator({
screens: {
Home: HomeStack,
Feed: Feed,
Notifications: Notifications,
},
});
const Navigation = createStaticNavigation(MyTabs);
export default function App() {
return <Navigation />;
}
function HomeStack() {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeStack} />
<Tab.Screen name="Feed" component={Feed} />
<Tab.Screen name="Notifications" component={Notifications} />
</Tab.Navigator>
</NavigationContainer>
);
}
With this structure, when we navigate to the Profile
or Settings
screen, the tab bar will still stay visible over those screens.
But if we want to show the tab bar only on the Home
, Feed
and Notifications
screens, but not on the Profile
and Settings
screens, we'll need to change the navigation structure. The easiest way to achieve this is to nest the tab navigator inside the first screen of the stack instead of nesting stack inside tab navigator:
- Static
- Dynamic
const HomeTabs = createBottomTabNavigator({
screens: {
Home: Home,
Feed: EmptyScreen,
Notifications: EmptyScreen,
},
});
const RootStack = createNativeStackNavigator({
screens: {
Home: HomeTabs,
Profile: EmptyScreen,
Settings: EmptyScreen,
},
});
function HomeTabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Feed" component={EmptyScreen} />
<Tab.Screen name="Notifications" component={EmptyScreen} />
</Tab.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeTabs} />
<Stack.Screen name="Profile" component={EmptyScreen} />
<Stack.Screen name="Settings" component={EmptyScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
After re-organizing the navigation structure, now if we navigate to the Profile
or Settings
screens, the tab bar won't be visible over the screen anymore.