Remove Header React Navigation v5
Asked Answered
G

4

8

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'
  }
}
Ghastly answered 28/2, 2020 at 2:12 Comment(3)
try this, hope help you......Brocade
Does this answer your question? How to hide react navigation header on one screenOverset
I was looking to disable it throughout the application. Selected answer provides both ways of doing it, on an individual basis and as a whole.Ghastly
R
24

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 :)

Rental answered 28/2, 2020 at 3:45 Comment(1)
@Ghastly Glad to help youRental
W
1

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.

Wolgast answered 14/10, 2020 at 20:34 Comment(0)
H
1

Remove in one screen:

      <Stack.Screen
              name="SignIn"
              component={SignInScreen}
              options={{
                headerShown: false,
              }}
            />

Remove in all stack:

     <Stack.Navigator
        screenOptions={{
         headerShown: false
        }}
        >
Homogeneity answered 4/8, 2021 at 17:47 Comment(0)
A
0

In react navigation V5, do add options={{ title: "Sign Up", animationEnabled: false, headerShown: false }}

  <AuthStack.Screen
  name="SignupScreen"
  component={SignupScreen}
  options={{ title: "Sign Up", animationEnabled: false, headerShown: false }}
/>
Asafoetida answered 28/8, 2020 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.