React Navigation Header Customization in RN using Navigation Options

Introduction

This is an example of React Navigation Header Customization in React Native using Navigation Options. In this example, we will see how to customize the Navigation Bar/ Navigation header. React Native does not provide any header by default, it comes when we add React Navigation to switch the activity.

Sometimes we have to customize the header according to the needs and we can easily do it with the help of navigation options. In this post, we will see the full customized header and customization of the right and left header.

Complete Header Customization using

static navigationOptions = ({ navigation }) => {
    //return header with Custom View which will replace the original header 
    return {
      header: (
        <View
          style={{
            height: 45,
            marginTop: 20,
            backgroundColor: 'red',
            justifyContent: 'center',
          }}>
          <Text
            style={{
              color: 'white',
              textAlign: 'center',
              fontWeight: 'bold',
              fontSize: 18,
            }}>
            This is Custom Header
          </Text>
        </View>
      ),
    };
  };

Left and Right Header Customization using

static navigationOptions = ({ navigation }) => {
  return {
    //Heading/title of the header
    title: navigation.getParam('Title', 'Left Right Custom Header'),
    //Heading style
    headerStyle: {
      backgroundColor: navigation.getParam('BackgroundColor', 'red'),
    },
    //Heading text color
    headerTintColor: navigation.getParam('HeaderTintColor', '#fff'),
    headerRight: (
      <TouchableOpacity onPress={() => alert('Right Menu Clicked')}>
        <Text
          style={{
            color: 'white',
          }}>
          Right Menu
        </Text>
      </TouchableOpacity>
    ),
    headerLeft: (
      <TouchableOpacity onPress={() => navigation.navigate('FirstPage')}>
        <Text
          style={{
            color: 'white',
          }}>
          Left Menu
        </Text>
      </TouchableOpacity>
    ),
  };
};

In this example,  We will create two screens and we will switch between them using React Navigation. On the first screen, you will see the full customized header and on the second screen, you will see the left and right header customization.

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

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 Structure

To start with this Example you need to create a directory named pages in your project and create two files FirstPge.js and SecondPage.js

Code

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

App.js

//This is an example code for the Custom Header//
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 SecondPage from './pages/SecondPage';
//import all the screens we are going to switch 
 
const App = createStackNavigator({
    FirstPage: { screen: FirstPage },
    SecondPage: { screen: SecondPage }, 
  },
  {
    initialRouteName: 'FirstPage',
  }
);
export default createAppContainer(App);

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

FirstPage.js

//This is an example code for the Custom Header//
import React, { Component } from 'react';
//import react in our code
import { View, Text, Button } from 'react-native';
//import all the components we are going to use.

class FirstPage extends Component {
  //Navigation option to create menu in header
  static navigationOptions = ({ navigation }) => {
    //return header with Custom View which will replace the original header 
    return {
      header: (
        <View
          style={{
            height: 45,
            marginTop: 20,
            backgroundColor: 'red',
            justifyContent: 'center',
          }}>
          <Text
            style={{
              color: 'white',
              textAlign: 'center',
              fontWeight: 'bold',
              fontSize: 18,
            }}>
            This is Custom Header
          </Text>
        </View>
      ),
    };
  };

  render() {
    const { navigate } = this.props.navigation;
    return (
      <View style={{ flex: 1, backgroundColor: '#ffdffd' }}>
        <View
          style={{ alignItems: 'center', flex: 1, justifyContent: 'center' }}>
          <Text
            style={{
              fontSize: 20,
              fontWeight: 'bold',
              textAlign: 'center',
            }}>
            This is an example of custom header
          </Text>
          <Button
            title="Click to see left right align header"
            onPress={() => {
              this.props.navigation.navigate('SecondPage');
            }}
          />
        </View>
      </View>
    );
  }
}
export default FirstPage;

Open SecondPage.js in any code editor and replace the code with the following code.

SecondPage.js

//This is an example code for the Custom Header//
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native';
//import all the components we are going to use.

export default class SecondPage extends Component {
  //Navigation option to create menu in header
  static navigationOptions = ({ navigation }) => {
    return {
      //Heading/title of the header
      title: navigation.getParam('Title', 'Left Right Custom Header'),
      //Heading style
      headerStyle: {
        backgroundColor: navigation.getParam('BackgroundColor', 'red'),
      },
      //Heading text color
      headerTintColor: navigation.getParam('HeaderTintColor', '#fff'),
      headerRight: (
        <TouchableOpacity onPress={() => alert('Right Menu Clicked')}>
          <Text
            style={{
              color: 'white',
            }}>
            Right Menu
          </Text>
        </TouchableOpacity>
      ),
      headerLeft: (
        <TouchableOpacity onPress={() => navigation.navigate('FirstPage')}>
          <Text
            style={{
              color: 'white',
            }}>
            Left Menu
          </Text>
        </TouchableOpacity>
      ),
    };
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View style={styles.container}>
        <Text>Left and Right Custom menu options</Text>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    margin: 50,
    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

        

Output in Online Emulator

This is how you can Customize Header using Navigation Options in React Native. 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

5 thoughts on “React Navigation Header Customization in RN using Navigation Options”

  1. its reallty awesome….

    but i have one dopubts,

    1. i i need to create react native app, so which way i need to follow,
    – react-native or react-native -web-app

    please tell me.

    Reply
    • If you want a native app then you should go for the React Native but if you want to build an app targeting web + mobile both then you can go for React Native Web. My experience with React Native Web is not that much good as it feels like an app cum web app.

      Reply
  2. I used the header component all over my app but when I go back from a screen the header component of the screen that just went away stays for sometime before moving away ! any fixes?

    Reply

Leave a Comment

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