React Native Navigation: Navigating Between Screens

To create Navigation in React Native, use the “StackNavigator”. The StackNavigator allows our app to transition between screens and manage navigation history. If our app uses only one StackNavigator, it is conceptually similar to how a web browser handles Navigation.

The main difference between how this works in a web browser and React Navigation is that React Navigation’s  StackNavigator provides the gestures and animations you expect on Android and iOS when navigating between routes in the stack.

Step 1: Install React Native

Now install the react native project by the following command.

sudo npm install -g react-native-cli

After that, we must generate the project and go into that directory.

react-native init RNNavigation
cd RNNavigation

I am showing you this example on the iOS device; we need to start the ios development. Hit the following command to generate an iOS build.

react-native run-ios

Step 2: Create two screens for our project.

In the root directory, create one folder called screens. Then, go into that folder and create two screens.

  1. Settings.js
  2. Home.js

Add the following code to the Settings.js file.

// Settings.js

import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';

export class Settings extends Component {
  render() {
    return (
      <View>
        <Text>This is the Settings screen</Text>
      </View>
    )
  }
};

export default Settings;

After that, add the following code to the Home.js file.

// Home.js

import React, { Component } from 'react';
import { View, Text } from 'react-native';

export class Home extends Component {
  render() {
    return (
      <View>
        <Text>This is the home screen</Text>
      </View>
    )
  }
}

export default Home

We have both screens ready. Next, we need to add Navigation to it.

Step 3: Install the React Navigation package.

Go to your project root, and in the terminal, add the package using the following command.

yarn add react-navigation

Add the react-navigation module inside the App.js file.

// App.js

import { StackNavigator } from 'react-navigation';

StackNavigator allows your app to transition between the screens and manage navigation history.

Import the two screens we have created inside the App.js file.

// App.js

import Settings from './screens/Settings';
import Home from './screens/Home';

Pass both screens on the StockNavigator function.

// App.js

const AppNavigator = StackNavigator({
  SettingScreen: { screen: Settings },
  HomeScreen: { screen: Home }
});

Our final App.js file looks like this.

// App.js

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import Settings from './screens/Settings';
import Home from './screens/Home';

const AppNavigator = StackNavigator({
  SettingScreen: { screen: Settings },
  HomeScreen: { screen: Home }
});

export default class App extends Component {
  render() {
    return (
      <AppNavigator />
    );
  }
}

Step 4: Add the Navigation Button inside the Settings.js file.

Write the following code inside the Settings.js file.

// Settings.js

export class Settings extends Component {
  render() {
    return (
      <View>
        <Text>This is the Settings screen</Text>
        <Button onPress={() => this.props.navigation.navigate('HomeScreen')} title="Home"/>
      </View>
    )
  }
};

So, we have done that when the user clicks that button, it will redirect to HomeScreen, which we have defined inside the App.js file.

So our Screen smoothly changes to the clicked one. In our case, it is HomeScreen. So, our final Settings.js file looks like this.

// Settings.js

import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';

export class Settings extends Component {
  render() {
    return (
      <View>
        <Text>This is the Settings screen</Text>
        <Button onPress={() => this.props.navigation.navigate('HomeScreen')} title="Home"/>
      </View>
    )
  }
};

export default Settings;

Step 5: Reload the application.

Reload the iPhone Simulator. You will see the Screen like this.

React Native Navigation Tutorial With Example From Scratch

Now, you can see on the Screen that a Home button is there, so after clicking that Home Button, we can transit through navigating to the new window.

Above the Screen is the Setting Screen, and after the transition, we get the Home Screen.

So Above example is the primary example of React Native Navigation. We will see the advanced-level examples in future posts. That’s it.

16 thoughts on “React Native Navigation: Navigating Between Screens”

  1. I am getting error: undefined is not a function(evaluating'(0,_reactNavigation.StackNavigator)({SettingScreen:{
    screen:_Settings.dedfault},HomeScreen:{screen:_Home.default}})’)
    Am new to React Native, in fact only been 2 days , didn’t understand what this error meant, I noticed that you haven’t added the extension .js to settings.js while importing , i tried adding it and still is getting the same error, help me plz

    Reply
  2. OK finally found the answer, the code shown above is for React-Native v2 which is outdated as of now by v3, many websites explain navigation in the old way, which makes it even harder for beginners to pinpoint the exact problem.
    In v2 and earlier, the containers in React Navigation are automatically provided by the create*Navigator functions. As of v3, you are required to use the container directly. In v3 we also renamed createNavigationContainer to createAppContainer.
    https://stackoverflow.com/questions/53367195/invariant-violation-the-navigation-prop-is-missing-for-this-navigator
    Author please update the code above ASAP, to avoid confusion among readers

    Reply
  3. Hello !

    Thank’s a lot for this tutorial.
    I have just a little issue (without error) : once i have the navigation in place, it doesn’t render any page, Settings or Home. He renders only a white page.

    Any idea to resolve this issue ?

    Thank’s again!

    Reply
  4. Hi! Thanks for the great tutorial!
    How can I send data from the homescreen to the settingsscreen if I tap “back” on my phone?
    I know i can add these to .navigate function, but in there is no implementation for the backbutton….

    Reply
  5. Hello, thanks for this useful information.
    This tutorial helps me to clear my navigation concept. I was stuck at some points but after more research that is resolved. Great job…

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.