Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Header React Navigation v5

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'
  }
}
like image 880
arthurk33 Avatar asked Feb 28 '20 02:02

arthurk33


2 Answers

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

like image 92
Akila Devinda Avatar answered Sep 23 '22 16:09

Akila Devinda


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.

like image 37
AlexanderD Avatar answered Sep 26 '22 16:09

AlexanderD