Skip to main content

Tell us about your project

Example React Native App with React navigation

In this article we will go through how to setup an app with StackNavigator, DrawerNavigator, TabNavigator and how to navigate using it.

First we'll start with initiatiating the App and installing dependencies:

$ react-native init tutorial && cd tutorial
$ yarn add react-navigation

We'll create a folder src/ where all our App logic will be located.

Inside we'll create a screens/ folder where we'll hold all our screens, then static/. In here we'll add our navigation structure in the router.js file. So let's create 6 files in the screen/ folder, named firstScreen.js, secondScreen.js,... sixthScreen.js, that will have nearly identical code but with minor changes.

This is the definition of our screens:

firstScreen.js

  1. import React, {Component} from 'react';
  2. import {View, Text, StyleSheet} from 'react-native';
  3. export default class FirstScreen extends Component {
  4.   render() {
  5.     return (
  6.       <view style="{styles.container}">
  7.         <text color:="" style="{{">Screen 1</text>
  8.       </view>
  9.     );
  10.   }
  11. }
  12. const styles = StyleSheet.create({
  13.   container: { flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: '#222' }
  14. });

for all other screens we should change lines:

  1.  export default class FirstScreen extends Component {
  2.    where we'll change FirstScreen to the screen we are adding: FirstScreen, SecondScreen,... SixthScreen
  3. }

Screen 1: here we will change number 1-6

You can change those values as you like, this is just to differentiate between different screens when navigating. You can change backgroundColor at line 21, to a different color for each screen.

Now that we have our screens, let's create our App navigation. DrawerNavigator which will have StackNavigator, with two screens, and TabNavigator. This TabNavigator will have TabNavigator inside. With this structure we can explore basic navigation between screens, showing/hiding drawer on screens, how tabs work .

First we create top most TabNavigator, or TabNavigator inside TabNavigator, we'll call it SecondaryTabs, and will get screens five and six. Tabs will be on top of screen and drawer will not show on those pages.

router.js

  1. import React from 'react';
  2. import { StackNavigator, DrawerNavigator, TabNavigator } from 'react-navigation';
  3. import { NavigationActions } from 'react-navigation';
  4. import FirstScreen from './../screens/firstScreen';
  5. import SecondScreen from './../screens/secondScreen';
  6. import ThirdScreen from './../screens/thirdScreen';
  7. import FourthScreen from './../screens/fourthScreen';
  8. import FifthScreen from './../screens/fifthScreen';
  9. import SixthScreen from './../screens/sixthScreen';
  10. const SecondaryTabs = TabNavigator({ ScreenFive: { screen: FifthScreen, navigationOptions: { tabBarLabel: 'Screen 5', } }, ScreenSix: { screen: SixthScreen, navigationOptions: { tabBarLabel: 'Screen 6', } } }, { tabBarPosition: 'top', });

Next we create a Primary Tabnavigator, that will have pages 3, 4 and 5, 6 in secondary TabNavigator, tabs will be on the bottom.

router.js

  1. const primaryTabs = TabNavigator({ screenThree:
  2. { screen: thirdScreen, navigationOptions: { tabBarLabel: 'Screen 3', } }, secondaryTabs: { screen: secondaryTabs, navigationOptions: { tabBarLabel: 'Screens 5/6', header: null } }, screenFour: { screen: fourthScreen, navigationOptions: { tabBarLabel: 'Screen 4', } } }, { tabBarPosition: 'bottom', });

Now we'll create a Drawer. Because we want header header in our TabNavigator without having to create and add a header component to each screen of the TabNavigator, we'll incase it in StackNavigator. Drawer will have another StackNavigator where we'll add screens 1 and 2 with mutual navigation separately from TabNavigator.

router.js

  1. const overAllNavigation = DrawerNavigator({ screenTab: { screen: StackNavigator({ Tabs: { screen: primaryTabs, navigationOptions: ({navigation}) =&gt; ({ title: 'Example App', headerLeft: { navigation.navigate('DrawerToggle') }}&gt;Menu }) } }) }, screenStack: { screen: StackNavigator({ screenOne: { screen: firstScreen, navigationOptions: ({navigation}) =&gt; ({ title: 'Screen 1', headerLeft: { navigation.navigate('DrawerToggle') }}&gt;Menu, headerRight: { navigation.navigate('screenTwo') }}&gt;Screen 2 }) }, screenTwo: { screen: secondScreen, navigationOptions: ({navigation}) =&gt; ({ title: 'Screen 2', headerLeft: { navigation.goBack() }}&gt;Back }) } }), navigationOptions: ({navigation}) =&gt; ({title: 'Screen 1/2'}) } }, { gesturesEnabled: false, drawerWidth: 240 });
  2. export default overAllNavigation;

The last thing is to change the index.js so this navigation is the main component of the App.

router.js

  1. Index.js import { AppRegistry } from 'react-native';
  2. import App from './src/static/router';
  3. AppRegistry.registerComponent('tutorial', () =&gt; App);

Example preview:

mobile app

Similar content can be found on: Cordova vs React Native