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

static navigationOptions = ({ navigation }) => {
  if (navigation.state.params && !navigation.state.params.fullscreen) {
    //Hide Header by returning null
    return { header: null };
  } else {
    //Show Header by returning header
    return {
      title: navigation.getParam('Title', 'First Page'),
      headerStyle: {
        backgroundColor: navigation.getParam('BackgroundColor', '#ED2525'),
      },
      headerTintColor: navigation.getParam('HeaderTintColor', '#fff'),
    };
  }
};

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:

onFullScreen() {
    // Set the params to pass in fullscreen isVisible to navigationOptions
	this.props.navigation.setParams({
	  fullscreen: !this.isVisible,
	});
	this.isVisible = !this.isVisible;
}

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

cd ProjectName

1. Install react-navigation dependency to import createAppContainer

npm install react-navigation --save

2. Other supporting libraries for react-navigation

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

3. Install react-navigation-stack to import createStackNavigator

npm install react-navigation-stack --save

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

CocoaPods Installation

Please use the following command to install CocoaPods

cd ios && pod install && cd ..

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.

Code

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

App.js

//Example to Hide Navigation Bar in React Native on Click of a Button//
import React, { Component } from 'react';
//import react in our code. 

//Import react-navigation
import { createAppContainer } from 'react-navigation';
import { createStackNavigator} from 'react-navigation-stack';
 
import FirstPage from './pages/FirstPage';
//import all the screens we are going to use
 
const App = createStackNavigator({
    FirstPage: { screen: FirstPage }, 
  },
  {
    initialRouteName: 'FirstPage',
  }
);
export default createAppContainer(App);

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

FirstPage.js

//Example to Hide Navigation Bar in React Native on Click of a Button//
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Button, Text } from 'react-native';
//import all the components we are going to use.

export default class FirstPage extends Component {
  constructor(props) {
    super(props);
    //Default visible isVisible
    this.isVisible = true;
  }

  static navigationOptions = ({ navigation }) => {
    if (navigation.state.params && !navigation.state.params.fullscreen) {
      //Hide Header by returning null
      return { header: null };
    } else {
      //Show Header by returning header
      return {
        title: navigation.getParam('Title', 'First Page'),
        headerStyle: {
          backgroundColor: navigation.getParam('BackgroundColor', '#ED2525'),
        },
        headerTintColor: navigation.getParam('HeaderTintColor', '#fff'),
      };
    }
  };

  onFullScreen() {
    // Set the params to pass in fullscreen isVisible to navigationOptions
    this.props.navigation.setParams({
      fullscreen: !this.isVisible,
    });
    this.isVisible = !this.isVisible;
  }

  render() {
    const { navigate } = this.props.navigation;
    return (
      <View style={styles.container}>
        <Text style={{ textAlign: 'center', fontSize: 18, margin: 16 }}>
          Hide App Header on Click of a Button
        </Text>
        <Button
          title={this.isVisible ? 'Hide Header' : 'Show Header'}
          onPress={() => this.onFullScreen()}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

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

Output Screenshots

IOS

   

Android

   

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. 🙂


Related Posts

Leave a Comment

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