React Native Drawer Navigation Component
In this article, we are going to see how to implement Drawer Navigation in react-native. For this, we are going to use createDrawerNavigator component. It is basically the UI panel that displays the navigation menu. It is hidden by default but will appear when the user swipes a finger from the edge of the screen. These days mobile apps are made up of a single screen, so create various navigation components in React Native. We want to use React Navigation.
const Drawer = createDrawerNavigator(); <Drawer.Navigator> <Drawer.Screen /> <Drawer.Screen /> </Drawer.Navigator>
Methods to open and close drawer:
- navigation.openDrawer(): It is used to open the drawer.
- navigation.closeDrawer(): It is used to close the drawer.
- navigation.toggleDrawer(): It is used to toggle the drawer.
Now let’s start with the implementation:
Step 1: Open your terminal and install expo-cli by the following command.
npm install -g expo-cli
Step 2: Now create a project by the following command.
expo init myapp
Step 3: Now go into your project folder i.e. myapp
Step 4: Now install react-navigation into your project. React Navigation is used to navigate between one page to another. Install it by using the following command.
npm install @react-navigation/native
Step 5: Now install dependencies into your react-native project by using the following command.
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
Step 6: Now install drawer from react-navigation.
npm install @react-navigation/drawer
For React Tab Navigation: This can be used in React Native as well
Example: Now let’s implement Drawer Navigation.
Start the server by using the following command.
npm run android
Output: If your emulator did not open automatically then you need to do it manually. First, go to your android studio and run the emulator. Now start the server again.