React Native Navigation (Router) Version 2+

React Native Navigation is used to switch from one screen to other in the desired manner. This navigation solution is written entirely in JavaScript (so you can read and understand all of the sources), on top of powerful native primitives.

React Navigation Version 3+ has been launched which needs some updates to navigate. This example will work for the React Navigation Version 2+. If you are using Version 3+ then please see our new post on React Native Navigation Version 3+.

Navigate Using

In this example, we will navigate from the first screen to second on a click of a button. So Let’s get Started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App.

Assuming that you have node installed, you can use npm to install the react-native-cli command line utility.
Open the terminal and go to the workspace and run Run the following commands to create a new React Native project This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use createStackNavigator you need to install react-navigation package. To install this

Open the terminal and jump into the project

Run the following command

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module the directory named react-navigation.

–save is optional, it is just to update the react-navigation dependancy in your package.json file.

Dependency Version for this Example used is

Project File Structure

To start with this Example you need to create a directory named pages in your project and create two files FirstPge.js and SecondPage.js in it.

Now Open App.js in any code editor and replace the code with the following code.

App.js

Open pages/FirstPage.js in any code editor and replace the code with the following code.

FirstPage.js

Open pages/SecondPage.js in any code editor and replace the code with the following code.

SecondPage.js

To Run the React Native App

Open the terminal again and jump into your project using. To run the project on an Android Virtual Device or on real debugging device or on the iOS Simulator by running

IOS

     

Android

     


That was the React Native Navigation. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

How useful was this post?

Click on a star to rate us!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

About Snehal Agrawal

#SelfLearner #MobileAppDeveloper #TechnologyMiner #OpenSourceLover thats enough to describe me..

View all posts by Snehal Agrawal →

4 Comments on “React Native Navigation (Router) Version 2+”

  1. when we run Navigation code in react-native then error at run time “undefined is not a function (evaluating stacknavigator——) how can we solve it.
    Please give solution.

    1. We have faced this problem usually with the
      “react-navigation”: “3.+” version

      Try to downgrade your react native version to make it work properly.
      “react-navigation”: “2.18.2”

      This example is made of using “2.18.2” version.

Leave a Reply

Your email address will not be published. Required fields are marked *

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