Skip to main content

React Native Drawer Navigation (createDrawerNavigator)

React Native Drawer Navigation is an UI panel which displays the app's navigation menu. By default it is hidden when not in use, but it appears when user swipes a finger from the edge of the screen or when user touches at the top of drawer icon added at app bar.
React Native Drawer Navigation imports the createDrawerNavigator from the react-navigation library.

  1. import { createDrawerNavigator } from 'react-navigation'  
It implements the createDrawerNavigator() to add the list of classes (screens).


  1. createDrawerNavigator(RouteConfigs, DrawerNavigatorConfig);  
To open and close the drawer, use the following helper methods:

  1. this.props.navigation.openDrawer();  
  2. this.props.navigation.closeDrawer();  
If you like to toggle your drawer then call the following method:

  1. this.props.navigation.toggleDrawer();  
Each of the above methods openDrawer()closeDrawer(), and toggleDrawer() are simply dispatching actions as:

  1. this.props.navigation.dispatch(DrawerActions.openDrawer());  
  2. this.props.navigation.dispatch(DrawerActions.closeDrawer());  
  3. this.props.navigation.dispatch(DrawerActions.toggleDrawer());  
Before creating the React Native drawer navigation, first go through the React Native Navigation where we discussed the react-navigation installation process.

React Native Drawer Navigation Example

Create two separate classes "DashboardScreen" and "WelcomeScreen" in the react native app to display on screen. Add these screens to createStackNavigator and add "md-menu" icon of 'react-native-vector-icons/Ionicons' package. On pressing the menu icon, call navigation.openDrawer() method to open drawer.
Now, import createDrawerNavigator from 'react-navigation' package and implement createDrawerNavigator(). After that add the stack navigation screen over it.

  1. import React, { Component } from 'react';  
  2. import { View, Text, StyleSheet, Button } from 'react-native';  
  3. import Icon from 'react-native-vector-icons/Ionicons';  
  4.   
  5. import {  
  6.     createSwitchNavigator,  
  7.     createAppContainer,  
  8.     createDrawerNavigator,  
  9.     createStackNavigator  
  10. } from 'react-navigation';  
  11. export default class App extends Component {  
  12.     render() {  
  13.         return <AppContainer />;  
  14.     }  
  15. }  
  16.   
  17. class WelcomeScreen extends Component {  
  18.     static navigationOptions = {  
  19.          title: 'Welcome',  
  20.     };  
  21.     render() {  
  22.         return (  
  23.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  24.                 <Text>WelcomeScreen</Text>  
  25.                 <Button  
  26.                     title="Go to DashboardScreen"  
  27.                     onPress={() => this.props.navigation.navigate('Dashboard')}  
  28.                 />  
  29.             </View>  
  30.         );  
  31.     }  
  32. }  
  33.   
  34. class DashboardScreen extends Component {  
  35.     static navigationOptions = {  
  36.          title: 'Dashboard',  
  37.     };  
  38.   
  39.     render() {  
  40.         return (  
  41.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  42.                 <Text>DashboardScreen</Text>  
  43.             </View>  
  44.         );  
  45.     }  
  46. }  
  47. const DashboardStackNavigator = createStackNavigator(  
  48.     {  
  49.         DashboardNavigator: DashboardScreen  
  50.     },  
  51.     {  
  52.         defaultNavigationOptions: ({ navigation }) => {  
  53.         return {  
  54.             headerLeft: (  
  55.                 <Icon  
  56.                     style={{ paddingLeft: 10 }}  
  57.                     onPress={() => navigation.openDrawer()}  
  58.                     name="md-menu"  
  59.                     size={30}  
  60.                 />  
  61.             )  
  62.         };  
  63.         }  
  64.     }  
  65. );  
  66.   
  67. const WelcomeStackNavigator = createStackNavigator(  
  68.     {  
  69.         WelcomeNavigator: WelcomeScreen  
  70.     },  
  71.     {  
  72.         defaultNavigationOptions: ({ navigation }) => {  
  73.             return {  
  74.                 headerLeft: (  
  75.                     <Icon  
  76.                         style={{ paddingLeft: 10 }}  
  77.                         onPress={() => navigation.openDrawer()}  
  78.                         name="md-menu"  
  79.                         size={30}  
  80.                     />  
  81.                 )  
  82.             };  
  83.         }  
  84.     }  
  85. );  
  86. const AppDrawerNavigator = createDrawerNavigator({  
  87.     Dashboard: {  
  88.         screen: DashboardStackNavigator  
  89.     },  
  90.     Welcome: {  
  91.         screen: WelcomeStackNavigator  
  92.     },  
  93. });  
  94.   
  95. const AppSwitchNavigator = createSwitchNavigator({  
  96.     Dashboard: { screen: AppDrawerNavigator },  
  97.     Welcome: { screen: WelcomeScreen },  
  98.   
  99. });  
  100.   
  101. const AppContainer = createAppContainer(AppSwitchNavigator);  
  102.   
  103. const styles = StyleSheet.create({  
  104.     container: {  
  105.         flex: 1,  
  106.         alignItems: 'center',  
  107.         justifyContent: 'center'  
  108.     }  
  109. });  

Output:

React Native Drawer Navigation React Native Drawer Navigation
React Native Drawer Navigation

Comments

Popular Posts

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Ultimate Folder Structure For Your React Native Project

  Ultimate Folder Structure For Your React Native Project React native project structure React Native is a flexible framework, giving developers the freedom to choose their code structure. However, this can be a double-edged sword for beginners. Though it offers ease of coding, it can soon become challenging to manage as your project expands. Thus, a structured folder system can be beneficial in many ways like better organization, simplified module management, adhering to coding practices, and giving a professional touch to your project. This write-up discusses a version of a folder arrangement that I employ in my React Native projects. This structure is based on best practices and can be modified to suit the specific needs of your project. Before we get into the project structure let’s give credit to @sanjay who has the original idea of the structure but I modify his version of the code, to make it better. Base library axios  — For network calling. react-navigation ...