Example of React Native Navigation Drawer with Latest Navigation V. 4+

This is an Example of React Native Navigation Drawer for Android and IOS. 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.

Make React Native Navigation Drawer Using

In this example, we will make a navigation drawer with Three screens. So let’s get started.

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

1. Install react-navigation dependency to import createAppContainer

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

3. Install react-navigation-drawer to import createDrawerNavigator

4. Install react-native-reanimated for the drawer animation (Used internally by react-navigation-drawer)

These steps are enough for the drawer navigation but in this example we are also using createStackNavigator to switch screeens, so please follow the next step also

5. 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 and react-native-reanimated.

Please use the following command to install CocoaPods

Project Structure

To start with this Example you need to create a directory named pages in your project and create three files Screen1.js, Screen2.js, and Screen3.js in it.

To have the drawer Icon in Action Bar you have to make an image directory in your project and have to copy any drawer image with name drawer.png (For this example. You can change if you want but you need to change the file name in code too).

Here is the drawer.png if you want to download.

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

App.js

Open pages/Screen1.js in any code editor and the Replace the code with the following code.

Screen1.js

Open pages/Screen2.js in any code editor and the Replace the code with the following code.

Screen2.js

Open pages/Screen3.js in any code editor and the Replace the code with the following code.

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

               

That was the React Native Navigation Drawer. 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. 🙂

77 thoughts on “Example of React Native Navigation Drawer with Latest Navigation V. 4+”

  1. Showing the error: undefined is not a function (evaluating ‘_this.props.navigationProps.toggleDrawer()’)
    toggleDrawer

    Can you please check this issue?

  2. Hello, I have this issues when trying to pass params to another screen, e.g. passing email from users list to user detail screen….

    TypeError: TypeError: undefined is not an object (evaluating ‘this.props.navigation.state.params.JSON_ListView_Clicked_Item’)

      • Actually I am. on the users list screen I have…..

        this.props.navigation.navigate(‘Profiles’, { Emailsss: item.email })} > {item.first_name} {item.last_name}

        and I get this error…

        TypeError: TypeError: undefined is not an object (evaluating ‘this.props.navigation.state.params.Emailsss’)

        I had to change from JSON_ListView_Clicked_Item to Emailsss to try fixing, but still not working.

        It Started happening since I implemented Drawer navigation.

  3. Actually I am. on the users list screen I have…..

    this.props.navigation.navigate(‘Profiles’, { Emailsss: item.email })} > {item.first_name} {item.last_name}

    and I get this error…

    TypeError: TypeError: undefined is not an object (evaluating ‘this.props.navigation.state.params.Emailsss’)

    I had to change from JSON_ListView_Clicked_Item to Emailsss to try fixing, but still not working.

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

  5. I already update react native navigation into 3+ but i also get error : undefined is not and object : this.props.navigationProps.toggleDrawer. please help

  6. null is not an object ( evaluating ‘RNGestureHandlerModule.Direction’)

    My app:
    “dependencies”: {
    “react”: “16.8.6”,
    “react-native”: “0.60.3”,
    “react-native-gesture-handler”: “^1.3.0”,
    “react-navigation”: “^3.11.1”
    }

    • Hi Rod,
      Not yet as React Native versions are changing very frequently almost in a month so we think the code base will expire over a time but still We are planning for that with some solution of this frequent changes.

  7. Thanks a lot, I was struggling to make it work. I was not sure if I had linked rn gesture handler correctly, but apparently I did. Just my code was not working but yours does so thanks a lot!

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

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

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

  10. null is not an object ( evaluating ‘RNGestureHandlerModule.Direction’)

    App Dependencies

    “dependencies”: {
    “react”: “16.8.6”,
    “react-native”: “0.60.5”,
    “react-native-gesture-handler”: “^1.4.1”,
    “react-navigation”: “^3.12.0”
    },

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

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

Leave a Comment

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