React Native Navigation (Router) Version 2+

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+.

Please use this example only for the React Navigation Version < 3.0 else you can see Switch from One Screen to another using React Navigation 3.+ (Router) for the updated code.

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

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

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.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

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. 🙂

4 thoughts 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.

Leave a Comment

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