Hide React Navigation Header on Press of a Button

Here is an Example to Hide React Navigation Header on Press of a Button. React Navigation always add a header bar in the top of your application according to the material design instruction of the Android and IOS. To hide NavigationBar/ ActionBar/ Topbar/ 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 set the navigation header using static navigation option like this:

So here you can see I have used a state param fullscreen which is responsible to return header. By default fullscreen is set to return navigation header and on click of a button we will change the state fullscreen to return header: null.
Function to be called on click of a button:

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 init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

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

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

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

Installation of Dependencies

This example is updated for the React Navigation 4.0+. For more about React Navigation 4 please have a look at Using React Navigation 4.0 in React Native apps.

To switch between the screens and to make a navigation header we need to add react navigation in our application.

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

1. Install react-navigation dependency to import createAppContainer

2. Install react-native-gesture-handler dependency to support gesture navigation

3. Install react-navigation-stack to import createStackNavigator

These commands will copy all the dependencies into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods.

In this example we need to install the pods for react-native-gesture-handler.

Please use the following command to install CocoaPods

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.

App.js

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

FirstPage.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

IOS

   

Android

   

This is how you can hide React Navigation header on Press of a Button. If you have any doubt 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.