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

React Native Status Bar

What is the status bar? The simplest and easiest answer to this question can be answered by looking at your mobile phone's top section. What can you see? Most probably, it's...

4 minutes read.

React Native Pressable

It manages all the touch inputs, and it is one of the core wrappers introduced in July 2022. The Two important features of pressable are listed below- onPressInonPressOut onpressIn: We already know the...

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

React Native components are the same as ordinary react components, but they are different in terms of rendering and styling. All react code lives in react components. They are independent...

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

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.

Basic knowledge of firebase for React Native

Firebase for react-native : Firebase is a mobile and web application development platform which is created in 2014 by google. It is a product of google which is offered to developers...

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

HTTP in React Native

Networking HTTP in React-native: we all are aware that react native is a javascript framework.HTTP is nothing but an API request, and you might think that what is the need for an...

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

TouchableHighlight in React Native

It is just a wrapper for making touch work properly, and it is the conventional way of handling touch gestures. Today's most future-proof solution is pressable API. pressable manages all...

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

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

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.

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

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

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.