Updated 6 March 2021
We are going to create a navigation drawer in the react-native app which supports both iOS and Android apps.
Will cover this blog in simple terms with a minimal example. The navigation drawer is playing an important role when you have a menu list to show the app user.
Users can easily navigate menu items and take action on them.
If you have not created a project then please create a project by run this command –
1 |
react-native init "Project Name" |
if using existing then please make sure all updated javascript files downloaded in the node_module folder, If not then update by –
1 |
npm install |
Before starting the Navigation drawer we have to install these dependencies.
1 2 3 4 5 6 7 8 9 |
//Mainly use this dep for nav drawer. npm install @react-navigation/drawer //These are the support dep which required in nav drawer other will throw errors npm install @react-navigation/native npm install react-native-reanimated npm install react-native-screens npm install react-native-safe-area-context npm install react-native-gesture-handler |
After installing these dependencies run –
1 |
react-native link --all |
Check the below example code in App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import * as React from 'react'; import { Button, View, Text } from 'react-native'; import { NavigationContainer } from '@react-navigation/native' import { createDrawerNavigator } from '@react-navigation/drawer'; const Drawer = createDrawerNavigator(); function WebkulECommerce({ navigation }) { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text style={{padding:12}}>{"Welcome to Webkul E-Commerce"}</Text> <Button onPress={() => navigation.navigate('MobikulMobile-Apps')} title="Go to Mobikul Mobile App" /> </View> ); } function MobikulMobileApps({ navigation }) { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text style={{padding:12}}>{"Welcome to Mobikul App Section"}</Text> <Button onPress={() => navigation.goBack()} title="Go back" /> </View> ); } export default class App extends React.Component { render() { return ( <NavigationContainer> <Drawer.Navigator initialRouteName="WebkulE-Commerce"> <Drawer.Screen name="WebkulE-Commerce" component={WebkulECommerce} /> <Drawer.Screen name="MobikulMobile-Apps" component={MobikulMobileApps} /> </Drawer.Navigator> </NavigationContainer> ); }; } |
Explanation -:
Run the app and check out the result.
For Android –
1 2 3 |
react-native run-android //or, open the android folder in Android-studio and run by play button. |
For iOS –
1 2 3 4 5 6 |
//Go to ios folder by terminal cd ios //install pod files by the command pod install |
After success pod installed open ios folder in File Explorer and start workspace file which will be open in X-Code studio.
Output screens –
Add this key – drawerPosition in Navigator.
1 2 3 4 5 6 |
<NavigationContainer> <Drawer.Navigator initialRouteName="WebkulE-Commerce" drawerPosition="right"> <Drawer.Screen name="WebkulE-Commerce" component={WebkulECommerce} /> <Drawer.Screen name="MobikulMobile-Apps" component={MobikulMobileApps} /> </Drawer.Navigator> </NavigationContainer> |
See more of our exciting blogs – https://mobikul.com/blog/
Reference – https://reactnavigation.org/docs/drawer-based-navigation/
Happy coding … </ :)>
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.