Hide React Navigation Header on Press of a Button

Hide React Navigation Header

Here is an Example to Hide React Navigation Header on the Press of a Button. React Navigation always adds a header bar at the top of your application according to the material design instruction of the Android and IOS. To hide NavigationBar/ ActionBar/ Header from the top of the Screen There are many ways and you can hide the header according to the situation.

If you haven’t visited our post 3 Ways to Hide Navigation Bar in React Native Application then I recommend visiting that post also. It contains 3 ways to hide the Navigation Bar using the static code or you can say that while rendering the view. But in this example, we will hide the navigation header dynamically on the press of a button.

To hide the navigation header on Press of a Button

To hide the header we will use the headerShown property of navigation options

navigation.setOptions({headerShown: false});

In this example, We will create a stack navigator with a single screen which will have a header and has a button to click. 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-gesture-handler, react-native-reanimated, react-native-screens and react-native-safe-area-context and @react-native-community/masked-view

npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view --save

3. For the Stack Navigator install

npm install @react-navigation/stack --save

Note: When you use a navigator (such as stack navigator), you’ll need to follow the installation instructions of that navigator for any additional dependencies. If you’re getting an error “Unable to resolve module”, you need to install that module in your project.

You might get warnings related to peer dependencies after installation. They are usually caused by incorrect version ranges specified in some packages. You can safely ignore most warnings as long as your app builds.

CocoaPods Installation

Please use the following command to install CocoaPods

npx pod-install ios

To finalize the installation of react-native-gesture-handler, add the following at the top (make sure it’s at the top and there’s nothing else before it) of your entry files, such as index.js or App.js:

import 'react-native-gesture-handler';

Note: If you skip this step, your app may crash in production even if it works fine in development.

Project File Structure

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


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


// Hide React Navigation Header on Press of a Button
// https://aboutreact.com/hide-header-on-button-click/

import 'react-native-gesture-handler';

import React from 'react';

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

import FirstPage from './pages/FirstPage';

const Stack = createStackNavigator();

const App = () => {
  return (
      <Stack.Navigator initialRouteName="FirstPage">
          name="First Page"
            title: 'First Page', //Set Header Title
            headerStyle: {
              backgroundColor: '#f4511e', //Set Header color
            headerTintColor: '#fff', //Set Header text color
            headerTitleStyle: {
              fontWeight: 'bold', //Set Header text style

export default App;

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


// Hide React Navigation Header on Press of a Button
// https://aboutreact.com/hide-header-on-button-click/

import React from 'react';
import {
} from 'react-native';

const FirstPage = ({navigation}) => {
  const hideHeader = () => {
    // Function to change navigation options
    navigation.setOptions({headerShown: false});

  return (
    <SafeAreaView style={{flex: 1, backgroundColor: 'white'}}>
      <View style={styles.container}>
        <Text style={styles.header}>
          Hide React Navigation Header on Press of a Button
        <Text>Click on the below button to hide header</Text>

        {/* Change header color to yellow */}
          <Text style={styles.TextStyle}>Hide Header</Text>
      <Text style={{textAlign: 'center', color: 'grey'}}>

export default FirstPage;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    padding: 16,
  header: {
    fontSize: 25,
    textAlign: 'center',
    marginVertical: 16,
  buttonStyle: {
    width: '100%',
    height: 40,
    padding: 10,
    backgroundColor: '#808080',
    borderRadius: 2,
    marginTop: 12,
  TextStyle: {
    color: '#fff',
    textAlign: 'center',

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

Download Source Code

Output Screenshots


Output in Online Emulator

This is how you can hide React Navigation header on the Press of a Button. 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. 🙂

Leave a Comment

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