React Native Side Drawer

In this part of the react-native tutorial, we will be learning about the react-native side drawer. We have already studied the status bar and various parts of react-native navigation, including params. Side drawers provide neatness of activity or menu in the sliding panel. When the user is not using it, it can be hidden, which is one of the major benefits of using react native side drawers. It makes navigation easier and clean.

Installing packages for react-native side drawers-

  • npm install --save react-native-sidebar
  • import { createDrawerNavigator } from '@react-navigation/drawer';

Let's create one side drawer for we react native app :

Firstly create three separate files inside the app named first.js,second.js and third.js.

App.js

import 'react-native-gesture-handler';
 import { createStackNavigator } from '@react-navigation/stack';
import * as React from 'react';
import {
  Button,
  View,
  Text,
  TouchableOpacity,
  Image
} from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
 
import FirstPage from './First';
import SecondPage from './Second';
import ThirdPage from './Third';
 
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
 
const NavigationDrawerStructure = (props)=> {
 
  const toggleDrawer = () => {


    props.navigationProps.toggleDrawer();
  };
 
  return (
    <View style={{ flexDirection: 'coloumn' }}>
      <TouchableOpacity onPress={()=> toggleDrawer()}>
      </TouchableOpacity>
    </View>
  );
}
 
function firstScreenStack({ navigation }) {
  return (
      <Stack.Navigator initialRouteName="FirstPage">
        <Stack.Screen
          name="First"
          component={FirstPage}
          options={{
            title: 'welcome', //Set Header Title
            headerLeft: ()=>
              <NavigationDrawerStructure
                navigationProps={navigation}
              />,
            headerStyle: {
              backgroundColor: '#f4511e', //Set Header color
            },
            headerTintColor: '#fff', //Set Header text color
            headerTitleStyle: {
              fontWeight: 'bold', //Set Header text style
            },
          }}
        />
      </Stack.Navigator>
  );
}


 
function secondScreenStack({ navigation }) {
  return (
    <Stack.Navigator
      initialRouteName="SecondPage"
      screenOptions={{
        headerLeft: ()=>
          <NavigationDrawerStructure
            navigationProps={navigation}
          />,
        headerStyle: {
          backgroundColor: '#cc00cc', //Set Header color
        },
        headerTintColor: 'black', 
        headerTitleStyle: {
          
        }
      }}>
      <Stack.Screen
        name="i am the second"
        component={SecondPage}
        options={{
          title: 'hello', //Set Header Title
          
        }}/>
      <Stack.Screen
        name="ThirdPage"
        component={ThirdPage}
        options={{
          title: 'hola', //Set Header Title
        }}/>
    </Stack.Navigator>
  );
}
function thirdScreenStack({ navigation }) {
  return (
      <Stack.Navigator initialRouteName="thirdPage">
        <Stack.Screen
          name="i am the third"
          component={ThirdPage}
          options={{
            title: 'welcome', 
            headerLeft: ()=>
              <NavigationDrawerStructure
                navigationProps={navigation}
              />,
            headerStyle: {
              backgroundColor: '#ff00ff', 
            },
            headerTintColor: 'white', 
            headerTitleStyle: {
              fontWeight: 'italic', 
            },
          }}
        />
      </Stack.Navigator>
  );
}
 
 
function App() {
  return (
    <NavigationContainer>
      <Drawer.Navigator
        drawerContentOptions={{
          activeTintColor: '#cc33ff',
          itemStyle: { marginVertical: 10 },
        }}>
        <Drawer.Screen
          name="I am the first"
          options={{ drawerLabel: 'go to first page' }}
          component={firstScreenStack} />
        <Drawer.Screen
          name="I am the second"
          options={{ drawerLabel: 'go to second page' }}
          component={secondScreenStack} />
          <Drawer.Screen
          name="I am the third"
          options={{ drawerLabel: 'go to third page' }}
          component={thirdScreenStack} />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}
 
export default App;

First.js code:

import * as React from 'react';
import {
  Button,
  View,
  SafeAreaView,
  Text,
} from 'react-native';
 
const FirstPage = ({ navigation }) => {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={{ flex: 1 , padding: 26,backgroundColor:'#ffe6ff'}}>
        <View
          style={{
            flex: 1,
            alignItems: 'center',
            justifyContent: 'center',
          }}>
          <Text
            style={{
              fontSize: 25,
              textAlign: 'center',
              marginBottom: 20
            }}>
            india is great :) we love our country.
          </Text>
         
          
        </View>
        <Text
          style={{
            fontSize: 20,
            textAlign: 'center',
            justifyContent:'center',
            color: 'black',
            paddingBottom: 20,
            fontWeight:'bold'
          }}>
 tutorial and example
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'black',
            fontWeight:'italic'
          }}>
         Happy learning ! all the best 
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'black',
            fontWeight:'italic'
          }}>
         Happy learning ! all the best 
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'black',
            fontWeight:'italic'
          }}>
         Happy learning ! all the best 
        </Text>
      </View>
    </SafeAreaView>
  );
}
 
export default FirstPage;

Second.js code :

import * as React from 'react';
import {
  Button,
  Text,
  SafeAreaView,
  View,
} 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: 30
            }}>
              india is great :) we love our country.
          </Text>
          
        </View>
         <Text
          style={{
            fontSize: 20,
            textAlign: 'center',
            justifyContent:'center',
            color: 'black',
            paddingBottom: 20,
            fontWeight:'bold'
          }}>
 tutorial and example
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'black',
            fontWeight:'italic'
          }}>
         Happy learning ! all the best 
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'black',
            fontWeight:'italic'
          }}>
         Happy learning ! all the best 
        </Text>
        
      </View>
    </SafeAreaView>
  );
}
 
export default SecondPage;

Third.js code:

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: 26 ,backgroundColor:'#e6f2ff'}}>
        <View
          style={{
            flex: 1,
            alignItems: 'center',
            justifyContent: 'center',
          }}>
          <Text
            style={{
              fontSize: 15,
              textAlign: 'center',
              marginBottom: 26,
              fontWeight:'bold'
            }}>
          we love india :))
          </Text>
         
        </View>
         <Text
          style={{
            fontSize: 20,
            textAlign: 'center',
            justifyContent:'center',
            color: 'black',
            paddingBottom: 20,
            fontWeight:'bold'
          }}>
 tutorial and example
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'black',
            fontWeight:'italic'
          }}>
         Happy learning ! all the best 
        </Text>
        <Text
          style={{
            fontSize: 16,
            textAlign: 'center',
            color: 'black',
            fontWeight:'italic'
          }}>
         Happy learning ! all the best 
        </Text>
        
      </View>
    </SafeAreaView>
  );
}
 
export default ThirdPage;

Output :

React native side drawer
React native side drawer
React native side drawer
React native side drawer

Explanation of code :

 In App.js code we have created three functions as we have three connected screen through stack navigator. They are first.js , second.js and third js. Here stack navigator plays an important role.

import { createDrawerNavigator } from '@react-navigation/drawer';

This library plays an important role in the creation of side drawers.Lets look at the construction code of side drawers-

<Drawer.Navigator
        drawerContentOptions={{
          activeTintColor: '#cc33ff',
          itemStyle: { marginVertical: 10 },
        }}>
        <Drawer.Screen
          name="I am the first"
          options={{ drawerLabel: 'go to first page' }}
          component={firstScreenStack} />
        <Drawer.Screen
          name="I am the second"
          options={{ drawerLabel: 'go to second page' }}
          component={secondScreenStack} />
          <Drawer.Screen
          name="I am the third"
          options={{ drawerLabel: 'go to third page' }}
          component={thirdScreenStack} />
      </Drawer.Navigator>

We wrapped the whole code in the navigation container and then used the drawer. Navigator will be creating a drawer tab. Each drawer screen will be responsible for options of the drawer, and inside that, we have called that screen's component, so whenever we click there, we will be directed to our respective screens.

The rest screens' codes are identical and simple as we have studied the stack navigator properly. We have only designed one only basic page in first,js,second.js, and thord.js, which is easy to understand as it contains only text components and styling.

Explanation of first.js code :

It is a simple page, and the code is easy to understand. We have wrapped the text component inside the safe area view, we have defined background screen color through hex code, and in the bottom part, we have again used text and styled it as per our desire. One header is given.

Rest two pages contain the same property, and they are identical.


Related Topics

TouchableOpacity in React Native

Touchable opacity is the most suited example if you want to make views respond to proper touch. If you want to control the opacity then it can be done by...

3 minutes read.

React Native Fonts

The font is one of the important essences of any good UI(User Interface). It is very important to have a clear and beautiful font in your app. In this module,...

4 minutes read.

React Native navigation bar and button

Here we will be focusing on header navigation. We will add buttons to the header, which is the best way to interact with a header. We have already learned about...

4 minutes read.

React Native Alert

In this part of the tutorial, we will learn about react-native's alert API. It is compatible with both android and IOS. What is API? Answer - API stands for Application programming interface,...

4 minutes read.

React Native Creating Components

React elements are where all React codes reside. The React elements are very similar to the regular React components, which have a unique style and offer contrast. Working with views When you...

3 minutes read.

Difference Between React.js and React Native

React js vs. React-native React native:  react-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. Facebook released it in 2015 and witnessed...

3 minutes read.

Advantages and Disadvantages of React Native

Advantages of using React Native It is observed that React Native stands out from most of the existing methods of cross-platform application development, like Ionic or Cordova, because React Native renders...

3 minutes read.

States in React Native

We can control data of components of react-native by state and props. We are familiar with props and used them multiple times previously. Props: This is used to customize components and...

3 minutes read.

React Native Side Drawer

In this part of the react-native tutorial, we will be learning about the react-native side drawer. We have already studied the status bar and various parts of react-native navigation, including...

5 minutes read.

React Native Tutorial

Introduction to react native : React-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. It is released by Facebook in 2015...

4 minutes read.

React Native Animations

Animation: animation is a method in which puppets, figures, drawings, etc., appear as moving creatures, making an illusion that things are moving. It is a unique way of expressing something...

6 minutes read.

React Native indicator

React native activity indicator : React native activity indicator is one of the react-native components. We all have seen it several times. It's the loader. Look at the picture below - So now...

4 minutes read.

Lifecycle of React Native Components

We are familiar with the word “lifecycle”. In human life, the lifecycle has three phases - birthlifedeath In react native its component lifecycle is almost the same as the human lifecycle. The...

6 minutes read.

Why to Learn React Native

The demand for smartphone applications is ever-changing. The elder ones disappear from our remembrances, and smartphones and new applications come each day. In this turbulent ecosystem, it is struggling for...

4 minutes read.

React Native Toast

Introduction: It is a message that will be displayed on the screen for a short span of time to interrupt the user. Primarily it is displayed by any user action....

5 minutes read.

React Native stack navigation

You must know about the stack data structure. Stack is one of the important data structures which works in the FILO(first in last out) method. As you know, mobile apps are...

4 minutes read.

React Native API

API API stands for an Application programming interface. API allows communication between two software and is also responsible for data sharing. It takes the asked data from the server and delivers...

6 minutes read.

How Does React Native Work

Working of React Native React Native is a framework that lets us write android and iOS mobile applications using JavaScript. It is interesting to see how easy it is to develop...

4 minutes read.

React Native Sound

It is a great way to connect to the user. Just a tiny "ting" of any email received, or some sound for deleting a file or sound for sending a...

8 minutes read.

What is a Bridge in React Native?

A React Native is generally composed of two fields or two parts: The first part is the code of JavaScript.The second one is native code. The ability to build a bridge between...

3 minutes read.