Example of Navigation Drawer with Section Menu in React Navigation 5.x

In react navigation 5.x there’s a lot of functionality has been changed. Since we know from the beginning react navigation is one of the most usable plugin for react native. React Navigation comes with many different architectures and one of them is Navigation Drawer also known as Drawer Navigator. I’ve already post a tutorial for creating navigation drawer but its old. Today we would going to learn about creating navigation drawer in react navigation latest 5.x version in react native both android and iOS platforms. We would use the Hamburger icon to show and hide the drawer navigator screen. So in this tutorial we would learn about Example of Navigation Drawer with Section Menu in React Navigation 5.x in React Native.

Contents in this project Example of Navigation Drawer with Section Menu in React Navigation 5.x in React Native:

1. The first step is download all the compulsory NPM libraries in your current react native project. So open your react native project Root directory in Command Prompt in Windows and Terminal in MAC OS. Execute below command to install React Navigation itself.

Screenshot:

Screenshot after finish installation:

2. Now we have to install its all supporting dependencies of react navigation like react-native-gesture-handler, react-native-reanimated, react-native-screens and react-native-safe-area-context and @react-native-community/masked-view. Execute below command to install them.

Screenshot:

Screenshot after done installation:

3. Now we have to install Stack Navigator library in our current project. So execute below command to install the stack navigator.

Screenshot:

Example of Navigation Drawer with Section Menu in React Navigation 5.xScreenshot after done installation of Stack Navigator:

4. Now we have to install the createDrawerNavigator library in our current react native project. Execute below command to install Navigation Drawer / Drawer Navigator in your project.

Screenshot:

Screenshot after done installation:

5. Now for Android our project is ready to use, but for iOS devices we have to install PODS in our iOS directory to link all the newly installed packages. This step is only for iOS ( MAC ONLY ). So execute below command to install POD in your iOS project.

here you go friends now your project is ready to use with navigation drawer / drawer navigator. It’s time to start coding for app.

6. Open your project’s main App.js file and import StyleSheet, View, Text, Image, TouchableOpacity, SafeAreaView, react-native-gesture-handler, NavigationContainer, createStackNavigator, createDrawerNavigator, DrawerContentScrollView and DrawerItem components in your react native project.

7. Creating a new functional component named as HamburgerIcon with props parameter. We would use this component to display the Hamburger icon at the top left side of navigation drawer screen. We would also set click functionality on this Icon so on its click the drawer sidebar will open and close.

Hamburger Icon we’re using :

8. Creating a component named as CustomSidebar. This is our custom sidebar with sectioned menu list. This component is purely dynamic and all the values of this component is directly coming form Drawer.Screen component.

Screenshot of Custom Sidebar:

9. Creating 3 Screen functional component named as HomeScreen, SecondScreen and ThirdScreen. These are the screens of our project.

10. Creating all Style Sheet for project.

11. Creating constant object of createStackNavigator and createDrawerNavigator named as Stack and Drawer. We would use these objects to creating Stack navigator and drawer navigator.

12. Creating 3 Stack Navigator functional component each unique for each screen. Named as HomeStack, SecondStack, ThirdStack.

  • initialRouteName : Screen component name which we want to open first on app start like home screen.
  • component : Used to call the component which we want to display as current screen.
  • title : To set screen activity title, display on header bar.
  • headerLeft : To show the hamburger icon at the left side of current screen in navigation header bar.
  • backgroundColor : Used to set background color of header bar.
  • headerTintColor : Used to set header title text color.

13. Creating export default function App. Inside the default function we would call the NavigationContainer -> Drawer.Navigator -> Drawer.Screen components.

  1. drawerContent : Here we would call our CustomSidebar component. It’ll allow us the open the custom sidebar component on drawer open functionality.
  2. drawerLabel : The label text shows in Category or section 1.
  3. groupName : Used to set Group name / section name / category name.
  4. activeTintColor : To set Label highlight color on current label or category selection.

Note: We have only define category with same name and automatically all the label under same category will be arranged.

14. Complete Source Code for App.js file:

Screenshots:

Example of Navigation Drawer with Section Menu in React Navigation 5.x Example of Navigation Drawer with Section Menu in React Navigation 5.x