React Native Navigation Drawer – Example using React Navigation V6

Navigation Drawer/Sidebar

This is an example of React Native Navigation Drawer for Android and IOS using React Navigation V6. We will use react-navigation to make a navigation drawer in this example. React Native Navigation Drawer is a very popular component in app development. It provides you to manage the number of app options in a very easy manner. A user can navigate from one screen to another screen very easily by just pulling out the drawer. Here is an example of a navigation drawer.

To Create React Navigation Drawer

<NavigationContainer>
  <Drawer.Navigator
    drawerContentOptions={{
      activeTintColor: '#e91e63',
      itemStyle: { marginVertical: 5 },
    }}>
    <Drawer.Screen
      name="FirstPage"
      options={{ drawerLabel: 'First page Option' }}
      component={firstScreenStack} />
    <Drawer.Screen
      name="SecondPage"
      options={{ drawerLabel: 'Second page Option' }}
      component={secondScreenStack} />
  </Drawer.Navigator>
</NavigationContainer>

In this example, we will make a navigation drawer with Three screens. 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 React Native Navigation Drawer 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 Drawer Navigator install

npm install @react-navigation/drawer --save

4. Now we need to install and configure install react-native-gesture-handler and react-native-reanimated libraries that is required by the drawer navigator:

npm install react-native-gesture-handler react-native-reanimated --save

To configure react-native-reanimated add Reanimated’s Babel plugin to your babel.config.js (Reanimated plugin has to be listed last.)

module.exports = {
  presets: [
    ...
  ],
  plugins: [
    ... ,
    'react-native-reanimated/plugin'
  ],
};

To configure 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 file, such as index.js or App.js

import 'react-native-gesture-handler';

Note: If you are building for Android or iOS, do not skip this step, or your app may crash in production even if it works fine in development. This is not applicable to other platforms.
5. These steps are enough for the drawer navigation but in this example, we are also moving between screens so we will also need Stack Navigator

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

Note: When you use a 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

Project Structure

To start with this Example you need to create a directory named pages in your project and create three files FirstPage.js, SecondPage.js, and ThirdPage.js in it.

ReactNavigationStructure

Code for Navigation Drawer

App.js

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

// React Native Navigation Drawer
// https://aboutreact.com/react-native-navigation-drawer/
import 'react-native-gesture-handler';

import * as React from 'react';

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

import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
import ThirdPage from './pages/ThirdPage';

const Stack = createNativeStackNavigator();
const Drawer = createDrawerNavigator();

const FirstScreenStack = () => {
  return (
      <Stack.Navigator
        initialRouteName="FirstPage"
        screenOptions={{headerShown: false}}
      >
        <Stack.Screen
          name="FirstPage"
          component={FirstPage}
        />
      </Stack.Navigator>
  );
}

const SecondScreenStack = () => {
  return (
    <Stack.Navigator
      initialRouteName="SecondPage"
      screenOptions={{headerShown: false}}
    >
      <Stack.Screen
        name="SecondPage"
        component={SecondPage}/>
      <Stack.Screen
        name="ThirdPage"
        component={ThirdPage}/>
    </Stack.Navigator>
  );
}

function App() {
  return (
    <NavigationContainer>
      <Drawer.Navigator
        screenOptions={{
          drawerStyle: {
            backgroundColor: '#c6cbef', //Set Drawer background
            width: 250, //Set Drawer width
          },
          headerStyle: {
            backgroundColor: '#f4511e', //Set Header color
          },
          headerTintColor: '#fff', //Set Header text color
          headerTitleStyle: {
            fontWeight: 'bold', //Set Header text style
          }
        }}>
        <Drawer.Screen
          name="FirstPage"
          options={{
            drawerLabel: 'First page Option',
            title: 'First Stack'
          }}
          component={FirstScreenStack} />
        <Drawer.Screen
          name="SecondPage"
          options={{
            drawerLabel: 'Second page Option',
            title: 'Second Stack'
          }}
          component={SecondScreenStack} />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}

export default App;

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

FirstPage.js

// React Native Navigation Drawer
// https://aboutreact.com/react-native-navigation-drawer/
import * as React from 'react';
import {
  Button,
  View,
  Text,
  SafeAreaView
} from 'react-native';

const FirstPage = ({ navigation }) => {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={{ flex: 1 , padding: 16}}>
        <View
          style={{
            flex: 1,
            alignItems: 'center',
            justifyContent: 'center',
          }}>
          <Text
            style={{
              fontSize: 25,
              textAlign: 'center',
              marginBottom: 16
            }}>
            This is the First Page under First Page Option
          </Text>
          <Button
            onPress={
              () => navigation.navigate('SecondPage')
            }
            title="Go to Second Page"
          />
          <Button
            onPress={
              () => navigation.navigate('ThirdPage')
            }
            title="Go to Third Page"
          />
        </View>
        <Text
          style={{
            fontSize: 18,
            textAlign: 'center',
            color: 'grey'
          }}>
          React Navigate Drawer
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'grey'
          }}>
          www.aboutreact.com
        </Text>
      </View>
    </SafeAreaView>
  );
}

export default FirstPage;

SecondPage.js

Open pages/SecondPage.js in any code editor and place the following code.

// React Native Navigation Drawer
// https://aboutreact.com/react-native-navigation-drawer/
import * as React from 'react';
import {
  Button,
  View,
  Text,
  SafeAreaView
} from 'react-native';

const SecondPage = ({ navigation }) => {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={{ flex: 1, padding: 16 }}>
        <View
          style={{
            flex: 1,
            alignItems: 'center',
            justifyContent: 'center',
          }}>
          <Text
            style={{
              fontSize: 25,
              textAlign: 'center',
              marginBottom: 16
            }}>
            This is Second Page under Second Page Option
          </Text>
          <Button
            title="Go to First Page"
            onPress={
              () => navigation.navigate('FirstPage')
            }
          />
          <Button
            title="Go to Third Page"
            onPress={
              () => navigation.navigate('ThirdPage')
            }
          />
        </View>
        <Text
          style={{
            fontSize: 18,
            textAlign: 'center',
            color: 'grey'
          }}>
          React Navigate Drawer
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'grey'
          }}>
          www.aboutreact.com
        </Text>
      </View>
    </SafeAreaView>
  );
}

export default SecondPage;

ThirdPage.js

Open pages/ThirdPage.js in any code editor and place the following code.

// React Native Navigation Drawer
// https://aboutreact.com/react-native-navigation-drawer/

import * as React from 'react';
import {
  Button,
  View,
  Text,
  SafeAreaView
} from 'react-native';

const ThirdPage = ({ route, navigation }) => {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={{ flex: 1, padding: 16 }}>
        <View
          style={{
            flex: 1,
            alignItems: 'center',
            justifyContent: 'center',
          }}>
          <Text
            style={{
              fontSize: 25,
              textAlign: 'center',
              marginBottom: 16
            }}>
            This is Third Page under Second Page Option
          </Text>
          <Button
            onPress={
              () => navigation.navigate('FirstPage')
            }
            title="Go to First Page"
          />
          <Button
            onPress={
              () => navigation.navigate('SecondPage')
            }
            title="Go to Second Page"
          />
        </View>
        <Text
          style={{
            fontSize: 18,
            textAlign: 'center',
            color: 'grey'
          }}>
          React Navigate Drawer
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'grey'
          }}>
          www.aboutreact.com
        </Text>
      </View>
    </SafeAreaView>
  );
}

export default ThirdPage;

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 Screenshot

NavigationDrawerScreenshot1   NavigationDrawerScreenshot2    NavigationDrawerScreenshot3     NavigationDrawerScreenshot4

Output in Online Emulator

So that was React Native Navigation Drawer – Example using React Navigation V5. 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!

I hope you liked it. 🙂

43 thoughts on “React Native Navigation Drawer – Example using React Navigation V6”

  1. How can i remove the header from inside pages like
    Screen1 List -> Screen Details is page where i need back button how i can achieve this also change the header title.

    Reply
  2. Thanks a lot excellent tutorial.I have a small doubt in my example i have created a custom drawer menu where top area will be occupied by main app icon and below will my drawer menu like home,about,settings… etc so my question is on press of the main icon i want to close the drawer menu and update the state of the home screen.

    Reply
  3. Hello Snehal,

    Thank you for the snippet.
    I tried to add multple indexes in a stackNavigator but there wasn’t any changes.
    Could you explain the utility to do that (if there is) and what should be the result.

    Reply
    • Hello Sevag, If you talk about the stack navigator then they are related to the single screen. In a single line, drawer navigation contains multiple stack navigator where each index of drawer navigator is a sidebar option and each index of the stack navigator is the multiple screens in the single option.
      If we have option1 with screen11, screen12, screen13, option2 with screen21, screen22, and option3 with screen31 then option1, option2, and option3 will be the drawer options and will come in drawer navigator but in the form of stack navigator with the same screen in a single option.

      Reply
  4. Great tutorial!! You helped me a lot!! 🙂 I spend so much time to find out how to make proper navigation drawer, any tutorial was working but yours finally did the job 😀 Thank you so much.

    Reply
  5. Hi Snehal Agrawal,

    It’s very help to me to learn react native.
    I have a question how to close side menu when clicking outside of side menu part link other screen?
    it’s working when clicking on other screen on side menu but i need to close when open side menu then click out side of side menu want to close.

    Reply
  6. I swear to god i looked for around 12 hrs trying to do this and none of the blogs helped me like this one. A big thanks to whoever wrote this post . Keep coding

    Reply
  7. Hi Snehal,

    Two days i was hardly trying to configure am getting the error as toggle drawer is not a function.Am not getting toggleDrawer or openDrawer or closeDrawer in my navigation props.Anything i did wrong ?Please help me to solve this issue

    Reply
    • See React is moving towards functional programming instead of the classes. Every library is updating itself so React Navigation also goes updated and this is one of the changes which they bought in the Navigation header. Instead of passing UI component directly you have to pass a functional component in header left.

      Reply
  8. Thank you !… Awesome post.
    i need to know how can i change this to,
    when different connection status for different colors in header style and side menu using NetInfo with it’s addEventListener. and need to know can i put header style, heder tint color As common.

    Reply
  9. how to make the menu tab at the bottom … I wanted to open the drawer from right to left and want to give the menu button at the bottom right …How to so that

    Reply

Leave a Comment

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