Adding a Drawer Navigation feature in your React Native app

Drawer Navigation is a useful UI panel that you can add to your app. It can increase the app’s UX. Let’s understand how you can get this feature in your app.

Most importantly for this project, you should consider the React Native framework. In this regard, we will have to become familiar with the prerequisites context. Before this, let’s understand what exactly is Drawer Navigation.

What is React Native’s Drawer Navigator? 

Drawer Navigator in React Native framework is a part of the UI. you can add this option to your app to make it more organized. Also, it is functionality, adding that you can navigate across the different screens in the app. 

You can hide several options in the drawer navigator. This way, you don’t have to add those menus or options in the main app interface. However, you have to create a button tapping which users can open the drawer navigator and access those embedded options. Also, you can design the drawer navigator in a way that users can access this option by swiping right from the left corner of the app screen. 

So, if you want to build a user-friendly navigation system in your app, you should consider a drawer navigator option.

Now, let’s move toward the prerequisites parameter. 

What are the prerequisite parameters?

There are three important steps that you can skip. These are as follows.

  1. Get the Environment of React Native– Don’t take me in the literal sense but what I mean to say is that you have to install software like Node.js, vs code editor and Android Studio. You can skip this setup process if you have already added React Native projects to your system. If not, check the attached blog to learn the setup procedures in detail created by experts of React Native app development company
  2. Build a React Native template– This is the step of project initialization where you have to create a basic file of React Native. I am referring to this file as the template. You can easily make this template by passing a simple command in the command prompt. Follow the below-mentioned steps.

Make a folder in your C drive → run cmd from this created folder→ type npx react-native init DrawerNavigator –version 0.68.1→ hit enter.

Your template will be successfully created. You can go to the initial folder created in the C drive to find this DrawerNavigator template in it. 

  1. Installing the third-party plugins– Third-party library support is one of the best functionality in React Native framework considering that you don’t have to write long lines of code. You can easily get components from the relevant libraries or plugins and use them on the codebase. 

The plugins that you have to get for this project are mentioned below. Also, the command of installation is given simultaneously. You have to run these commands one after the other in your project terminal. 

  • @react-navigation/native- The command of its installation is npm i @react-navigation/native
  • @react-navigation/drawer- The command of its installation is npm i @react-navigation/drawer

Codebase support for the project

Here, you need to rate the code syntax for three files. One of the main files is the App.js file. The other two are the screens that you need to create for the screen navigation. 

For this, you have to run cmd from your template folder. Then type code ., which will open the code editor from your project. 

Home Page 

The first one is the Home Page that we have to create. First, create a file in your editor with the name ‘Home.js’. 

Image 1

For this file, we will need the native components of React Native. These are Text, View, Image, and StyleSheet. The Text component is for adding text elements. The View component is for adding the main container and wrapping the text and Image element. So, we have imported these components from the react-native library. Along with this, you have to import the entire React library.

Going forward, the code introduces a Home component under which it returns the View and Image elements. Both the View and Image components have styling parameters attached to them. These are not defined in the codebase but are the standard styling parameters. 

Lastly, you have to export the component Home. 

Image 2

This is about considering the StyleSheet component of React Native. You can consider the styling objects of the container and image with the parameters of flex, width, and height. 

As we have built a simple Home page, we have to create another screen, i.e., the About screen.

About Screen

For this, make a file and name it About.js in your code editor. 

Image 3

Start with importing all the components that you need to build a basic screen. You will need Text, View, Image, and StyleSheet components for the file. 

Same as that of the Home Page, the code, here will start with specifying the main component About. 

Since we are focused on showing the use case of a Drawer navigator in this project, we won’t put much effort into adding designs to the screen.

Keeping the Image source and the code syntax the same as that of the Home.js file, we will proceed toward the next section, i.e., the App.js file. 

Also, don’t forget to export the About component. 

Image 4

This is the part of styling the screen. It is the same as that of the Home page. 

App.js file

This is the main file in every project. You have to embed the main operation in this file. In this regard, we will create the drawer navigator and add the two screen navigation methods. 

Image 5

Get two components from the third-party React Native plugin. These are the createDrawerNavigator and NavigationContainer imported from @react-navigation/drawer and @react-navigation/native respectively. 

Also, take the Home and About components that you have exported in the previous two sections. 

Image 6

Here, you have to add the createDrawerNavigator component as a function under which you have to add the <NavigationContainer> tag. 

Styling elements are added for the drawer, header, and drawer label. 

Image 7

As you can see, you have to embed the two screens under the Drawer.Screen element. Also, add unique Home and About components to each of these two screens. 

In the end, export the App. 

Image 8

Here, we have not used the StyleSheet component but have specified the component.

Hence, we have completed the section of the codebase. 

Final segment: To run the app on a device

You need to run the project on an emulator.

For this, run a new terminal and pass npm install. Also, pass npx react-native run-android on the terminal. 

Wait for the bundling to complete. Your emulator will start after it is done. Refer to the image 9 for the final output on the emulator.

Image 9

Hope, you have learned the concept of the drawer navigator and its use case in a real app. 

Related Articles

Leave a Comment