Skip to main content

React Native Navigation

React Native Navigation is used for managing the presentation, and transition between multiple screens. There are two types of navigation built in mobile applications. These are stack navigation and tabbed navigation patterns.

React Navigation Installation

React Navigation is generated from React Native community that provides the navigation solution written in JavaScript.
Create the React Native project and install the required packages and dependencies.

Install the react-navigation package in React Native project using any one of the below command.

  1. yarn add react-navigation  
  2. # or with npm  
  3. # npm install --save react-navigation  

After successful execution of the above command, it adds the "react-navigation": "^3.3.0" (3.3.0 is version) in package.json file.
After that, install react-native-gesture-handler package.

  1. yarn add react-native-gesture-handler  
  2. # or with npm  
  3. # npm install --save react-native-gesture-handler  

Now, link all the native dependencies together using command:

  1. react-native link react-native-gesture-handler  

To complete the installation of 'react-native-gesture-handler' for Android, make the following modification in MainActivity.java.
projectDirectory/android/app/src/main/java/com/project/MainActivity.java

  1. import com.facebook.react.ReactActivityDelegate;  
  2. import com.facebook.react.ReactRootView;  
  3. import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;  
  4.   
  5. . . .  
  6.   
  7. @Override  
  8. protected ReactActivityDelegate createReactActivityDelegate() {  
  9.     return new ReactActivityDelegate(this, getMainComponentName()) {  
  10.         @Override  
  11.         protected ReactRootView createRootView() {  
  12.             return new RNGestureHandlerEnabledRootView(MainActivity.this);  
  13.         }  
  14.     };  
  15. }  

Creating a stack navigator

To create a stack navigation, we import createStackNavigator and createAppContainer functions of react-navigation library.

App.js

  1. import React from "react";  
  2. import {Button, View, Text } from "react-native";  
  3. import { createStackNavigator, createAppContainer } from "react-navigation";  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     render() {  
  7.         return (  
  8.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  9.                 <Text>Home Screen</Text>  
  10.             </View>  
  11.         );  
  12.     }  
  13. }  
  14.   
  15. const AppNavigator = createStackNavigator({  
  16.     Home: {  
  17.         screen: HomeScreen  
  18.     }  
  19. });  
  20. export default createAppContainer(AppNavigator);  

The createStackNavigator is a function which takes a route configuration object and options object. It returns the React component.

MainActivity.java

  1. package com.naviexe;  
  2.   
  3. import com.facebook.react.ReactActivity;  
  4. import com.facebook.react.ReactActivityDelegate;  
  5. import com.facebook.react.ReactRootView;  
  6. import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;  
  7.   
  8. public class MainActivity extends ReactActivity {  
  9.   
  10.     @Override  
  11.     protected String getMainComponentName() {  
  12.         return "naviExe";  
  13.     }  
  14.     @Override  
  15.     protected ReactActivityDelegate createReactActivityDelegate() {  
  16.         return new ReactActivityDelegate(this, getMainComponentName()) {  
  17.             @Override  
  18.             protected ReactRootView createRootView() {  
  19.                 return new RNGestureHandlerEnabledRootView(MainActivity.this);  
  20.             }  
  21.         };  
  22.     }  
  23. }  

When we run the above code, we see an empty navigation bar containing the HomeScreen component.

Output:

React Native Navigation

Shorthand route configuration

When we have only a single screen as route that is Home screen component, we do not need to use the {screen: HomeScreen} , we can directly use the home component.

  1. const AppNavigator = createStackNavigator({  
  2.     Home: HomeScreen  
  3. });  

Adding title and styling navigation

  1. static navigationOptions = {  
  2.     title: 'Home',  
  3.     headerStyle: {  
  4.         backgroundColor: '#f4511e',  
  5.     },  
  6.     headerTintColor: '#0ff',  
  7.     headerTitleStyle: {  
  8.        fontWeight: 'bold',  
  9.     },  
  10. };  
  1. import React from 'react';  
  2. import { View, Text } from 'react-native';  
  3. import { createStackNavigator, createAppContainer } from 'react-navigation';  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     static navigationOptions = {  
  7.         title: 'Home',  
  8.         headerStyle: {  
  9.             backgroundColor: '#f4511e',  
  10.         },  
  11.       //  headerTintColor: '#0ff',  
  12.         headerTitleStyle: {  
  13.            fontWeight: 'bold',  
  14.         },  
  15.     };  
  16.     render() {  
  17.         return (  
  18.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  19.                 <Text>Home Screen</Text>  
  20.             </View>  
  21.         );  
  22.     }  
  23. }  
  24. const AppNavigator = createStackNavigator({  
  25.     Home: HomeScreen  
  26. });  
  27.   
  28. export default createAppContainer(AppNavigator);  

Adding a second route screen

Create another class (ProfileScreen) in App.js file to add the second route screen to the stack navigator.

  1. class ProfileScreen extends React.Component {  
  2.     render() {  
  3.         return (  
  4.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  5.                 <Text>Details Screen</Text>  
  6.             </View>  
  7.         );  
  8.     }  
  9. }  
  10.   
  11. const AppNavigator = createStackNavigator(  
  12.     {  
  13.         Home: HomeScreen,  
  14.         Profile: ProfileScreen  
  15.     },  
  16.     {  
  17.         initialRouteName: "Home"  
  18.     }  
  19. );  

The initialRouteName options object specifies the initial route in the stack navigation.

Complete code:

App.js

  1. import React from 'react';  
  2. import { View, Text } from 'react-native';  
  3. import { createStackNavigator, createAppContainer } from 'react-navigation';  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     render() {  
  7.         return (  
  8.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  9.                 <Text>Home Screen</Text>  
  10.             </View>  
  11.         );  
  12.     }  
  13. }  
  14. class ProfileScreen extends React.Component {  
  15.     render() {  
  16.         return (  
  17.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  18.                 <Text>Details Screen</Text>  
  19.             </View>  
  20.         );  
  21.     }  
  22. }  
  23.   
  24. const AppNavigator = createStackNavigator(  
  25.     {  
  26.         Home: HomeScreen,  
  27.         Profile: ProfileScreen  
  28.     },  
  29.     {  
  30.         initialRouteName: "Home"  
  31.     }  
  32. );  
  33.   
  34. export default createAppContainer(AppNavigator);  

Output:

React Native Navigation

In the next section, we will learn how to go from Home route to Profile route (one screen to another).

Comments

Popular Posts

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...