I can't seem to configure header to be null in the new version of React Navigation. I can set it to transparent, using the headerTransparent option, but this it looks like the header is still there, as the screen still requires a name.
Here is what I had initially, using the template that comes with a new Expo application
And this is what it looks like with the header as transparent. Which is essentially what I want to see but the title is still forced in there.
I don't want a header with my navigation, but that looks like the default behavior. I tried looking through the documentation to see if there was such a prop to delete the header but encountered a 404 page for options: https://reactnavigation.org/docs/en/navigation-options.html
I'm new to React Native, so there may be something I am misunderstanding. But the documentation is unclear on this and I couldn't find a stackoverflow question directly addressing this.
Here is my App.js
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import BottomTabNavigator from './navigation/BottomTabNavigator';
import useLinking from './navigation/useLinking';
const Stack = createStackNavigator();
........
<NavigationContainer ref={containerRef} initialState={initialNavigationState}>
<Stack.Navigator>
<Stack.Screen name="root" component={BottomTabNavigator} options={{headerTransparent: true}}/>
</Stack.Navigator>
</NavigationContainer>
Here is my BottomTabNavigator.js, which is very similar to the template code that expo provides.
import * as React from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import TabBarIcon from '../components/TabBarIcon';
import HomeScreen from '../screens/Home';
import SearchScreen from '../screens/Search';
const BottomTab = createBottomTabNavigator();
const INITIAL_ROUTE_NAME = 'Home';
export default function BottomTabNavigator({ navigation, route }) {
navigation.setOptions({ headerTitle: getHeaderTitle(route) });
return (
<BottomTab.Navigator initialRouteName={INITIAL_ROUTE_NAME}>
<BottomTab.Screen
name="Home"
component={HomeScreen}
options={{
title: 'Home',
tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} name="md-home" />
}}
/>
<BottomTab.Screen
name="Search"
component={SearchScreen}
options={{
title: 'Search',
tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} name="md-search" />,
}}
/>
</BottomTab.Navigator>
);
}
function getHeaderTitle(route) {
const routeName = route.state?.routes[route.state.index]?.name ?? INITIAL_ROUTE_NAME;
switch (routeName) {
case 'Home':
return 'How to get started';
case 'Appointments':
return 'Your appointments';
case 'Search':
return 'Search for services';
case 'Account':
return 'Account'
}
}
In your scenario, you have two options. Either you can disable header for all screens or you can disable header for the selected screen only.
For disable header for all-around your application edit your app.js like this
App.js
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import BottomTabNavigator from './navigation/BottomTabNavigator';
import useLinking from './navigation/useLinking';
const Stack = createStackNavigator();
........
<NavigationContainer ref={containerRef} initialState={initialNavigationState}>
<Stack.Navigator screenOptions={{headerShown: false,}}>
<Stack.Screen name="root" component={BottomTabNavigator}/>
</Stack.Navigator>
</NavigationContainer>
You need to pass screenOptions into Stack.Navigator and make headerShown:false
Assume that you want to disable header on specific screen only, then this example will help you
<Stack.Navigator ...>
...
<Stack.Screen
name="Landing"
component={LandingScreen}
options={{
headerShown: false, // change this to `false`
}}
/>
...
</Stack.Navigator>
Hope you have clear idea about this :)
Setting headerMode: none
as a default prop will remove it from any screen.
const Stack = createStackNavigator();
Stack.Navigator.defaultProps = {
headerMode: 'none',
};
Also, I think that you can set the screenOptions's headerShown prop to false as a defaultProp too, but it would be like hiding the header on each screen rather than doing it once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With