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.
- Settings.js
- 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.
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.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
sir awesome tutorial but i need to set homepage as opening page rather than setting page.
check this :
const AppNavigator = StackNavigator({
HomeScreen: { screen: Home },
SettingScreen: { screen: Settings }
});
Thanks
Unable to resolve “react-navigation” from “App.js” any solution?
npm i react-navigation
nice!
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
getting same error here..i didn’t understand what to do.
The StackNavigator function name is deprecated, please use createStackNavigator instead
Refer: https://github.com/react-navigation/react-navigation/issues/4134
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
I am getting error: Failed to load bundle
Any one help me in this situation?
I am getting error: null is not an oject(evaluating ‘RNGestureHandlerModule.State)
i cant run
and show this error:
Only one default export allowed per module. (134:0)
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!
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….
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…