How to do Hamburger Menu in React-Native (Android) Application

Hi,

I will try to explain “How to do Hamburger menu in React-Native? (Android)” in this post. Just android because, I couldn’t test on iOS application yet. I used windows computer in this application. Let’s get start to development.

I assume, you know “How to create a new project in react-native” I skipped this step. I use Visual Studio Code for my react-native applications. You can use this IDE or you can find alternatives. I will use screenshots from VS Code in this article. After create project, I created Pages folder for my screens. This folder is optional. You can use alternatives.(It’s mean different folder name) But I will create my all pages in this folder.

After create my example pages, I included some libraries in my project. You should run respectively as follow commands in command prompt.

npm install native-base –save
react-native link
npm install react-native-elements

After included this libraries, I created empty pages. (for hamburger menu items) My example page names are “HomePage.js, ProfilePage.js, SettingsPage.js, NotificationPage.js” Again, you can use different page names in here.

Empty Page Example

I shared my example page source code’s ın screenshots. You can find GitHub links end of this post. I didn’t add any component in this page. Just a page name for test.

In this step, I will include navigation library to my project. You should run this command in command prompt for this.

npm i react-navigation

After included react-navigation library, I cleared App.js file and I designed my navigation menu in this file. First step for design, I added my libraries and pages to App.js file.

Include libraries and pages

Finally, everything is ready for hamburger menu. Let’s start to design hamburger menu. If you know web programming you can think as html. It’s easy. Before share my design code block, let me explain to you my sections.

I added logo area for application and then text area for username or application name.

After then, I added DrawerItems tag for menu items. My pages will be show in via this items.

Finally, I added two icon for contact, information or etc. This icons are clickable and will be in bottom page.

I created assets folder in my root directory. If I don’t, application will give an error. After create assets folder, I copied no-image.png to this folder. I used this in hamburger menu. (Logo area) My designed codes are as follows.

Hamburger design codes.

I defined my pages when design finished. You can think Select – Option. I used this items in my design. My items screenshot as follows.

DrawerNavigator items.

If you want to add more item, you should insert more code block as follow.

Single item example.

After above step finished, I added code in bottom of App.js file as follow.

Menu finished. If I try to start my application after this step, I will see as follows error.

Error example

The solution is simple. Before I run my application (or after stopped via CTRL + C in command prompt) and then run this code in command prompt.

npm install –save react-native-gesture-handler
react-native link react-native-gesture-handler

After included and linked this package I can start application in virtual device. (Genymotion or etc.) I can see my hamburger menu like as follows.

Hamburger menu v1

Now, my menu ready for use but I will add icon for my items. It’s not required but It will be good with this icons. Before we added native-base library to current project. I used this library in this step. This code block should be over to Render method. This code block’s screenshot as follows. (static navigationOptions)

You can use https://ionicframework.com/docs/ionicons/ for menu icons. You should change name properties in above code. Finally, our project is as follows.

You can find project code in here.

Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *