3 Ways to Hide Navigation Bar in React Native Application

3 Ways to Hide Navigation Bar

Here are the 3 Ways to Hide Navigation Bar in React Native Application. If you are making an application with a React Navigation StackNavigator, you can find a NavigationBar/ ActionBar on the top of the screen. If you are on the first screen of the StackNavigator it will show simple NavigationBar but if you navigate to the next screen then it will show you the back arrow to go back.

Sometimes you don’t want to have a navigation bar on the top of the screen so to hide the navigation bar we can use headerShown: false.

To hide the header here are the following options.

Options to Hide Navigation Bar

1. For the single screen, you can set headerShown: false in navigation options using useLayoutEffect hook

React.useLayoutEffect(() => {
  navigation.setOptions({headerShown: false});
}, [navigation]);

2. For the single screen, you can set headerShown:null in Stack.Screen

<Stack.Screen
  name="Home"
  component={HomeActivity}
  options={{headerShown: false}}
/>

3. Hide the header for all the screens in once using screenOptions in Stack.Navigator

<Stack.Navigator
  initialRouteName="HomeActivity"
  screenOptions={{headerShown: false}}
>
  <Stack.Screen>
    .......
  </Stack.Screen>
</Stack.Navigator>

If you want to hide the header dynamically on click of a button then you can visit our post on Hiding React Navigation Header on Press of a Button.

In this example, you can see how can you hide the header of any screen. Please find the comments to see how can you hide the header in different ways. We will use react-navigation to make the navigation bar on the top of the screen. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

For navigation we need to add react-navigation and other supporting dependencies.

To install the dependencies open the terminal and jump into your project

cd ProjectName

1. Install react-navigation

npm install @react-navigation/native --save

2. Other supporting libraries react-native-screens and react-native-safe-area-context

npm install react-native-screens react-native-safe-area-context --save

react-native-screens package requires one additional configuration step to properly work on Android devices. Edit MainActivity.java file which is located in android/app/src/main/java/<your package name>/MainActivity.java.

Add the following code to the body of MainActivity class:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(null);
}

and make sure to add the following import statement at the top of this file below your package statement:

import android.os.Bundle;

This change is required to avoid crashes related to View state being not persisted consistently across Activity restarts.

3. For the Stack Navigator install

npm install @react-navigation/native-stack --save

CocoaPods Installation

Please use the following command to install CocoaPods

npx pod-install

Project File Structure

To start coding you need to create a directory named pages in your project and create a file HomeActivity.js in it.

Code

Now open App.js in any code editor and replace the code with the following code

App.js

// 3 Ways to Hide Navigation Bar in React Native Application
// https://aboutreact.com/react-native-hide-navigation-bar-and-make-screen-full-screen/

import React from 'react';

import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';

import HomeActivity from './pages/HomeActivity';

const Stack = createNativeStackNavigator();

const App = () => {
  return (
    <NavigationContainer>
      <Stack.Navigator
        initialRouteName="HomeActivity"
        //screenOptions={{headerShown: false}}
      >
        <Stack.Screen
          name="Home"
          component={HomeActivity}
          //options={{headerShown: false}}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default App;

Open pages/HomeActivity.js in any code editor and replace the code with the following code.

HomeActivity.js

// 3 Ways to Hide Navigation Bar in React Native Application
// https://aboutreact.com/react-native-hide-navigation-bar-and-make-screen-full-screen/

import React from 'react';
import {SafeAreaView, StyleSheet, Text, View} from 'react-native';

const HomeActivity = ({navigation, route}) => {
  React.useLayoutEffect(() => {
    navigation.setOptions({headerShown: false});
  }, [navigation]);

  return (
    <SafeAreaView style={{flex: 1, backgroundColor: 'white'}}>
      <View style={styles.container}>
        <Text style={styles.header}>
          3 Ways to Hide Navigation Bar
          {'\n'}
          in React Native Application
        </Text>
      </View>
      <Text style={{textAlign: 'center', color: 'grey'}}>
        www.aboutreact.com
      </Text>
    </SafeAreaView>
  );
};

export default HomeActivity;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    padding: 16,
  },
  header: {
    fontSize: 25,
    textAlign: 'center',
    marginVertical: 16,
  }
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device:

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

Output Screenshots

Output in Online Emulator

This is how you can hide Navigation Bar and make Screen Full Screen. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

2 thoughts on “3 Ways to Hide Navigation Bar in React Native Application”

    • As per the official site they suggested
      header – now accepts a function returning react element instead, use headerShown: false instead of header: null to hide the header.

      Updated the example for `headerShown: false`

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.