Skip to main content
Version: 1.x

Hello React Navigation

In a web browser, you can link to different pages using an anchor (<a>) tag. When the user clicks on a link, the URL is pushed to the browser history stack. When the user presses the back button, the browser pops the item from the top of the history stack, so the active page is now the previously visited page. React Native doesn't have a built-in idea of a global history stack like a web browser does -- this is where React Navigation enters the story.

React Navigation's StackNavigator provides a way for your app to transition between screens and manage navigation history. If your app uses only one StackNavigator then it is conceptually similar to how a web browser handles navigation state - your app pushes and pops items from the navigation stack as users interact with it, and this results in the user seeing different screens. A key difference between how this works in a web browser and in React Navigation is that React Navigation's StackNavigator provides the gestures and animations that you would expect on Android and iOS when navigating between routes in the stack.

All we need to get started using React Navigation is a StackNavigator.

Creating a StackNavigator

StackNavigator is a function that returns a React component. It takes a route configuration object and, optionally, an options object (we omit this below, for now). Because the StackNavigator function returns a React component, we can export it directly from App.js to be used as our App's root component.

// In App.js in a new project

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

class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}
}

export default StackNavigator({
Home: {
screen: HomeScreen,
},
});

→ Run this code

If you run this code, you will see a screen with an empty navigation bar and a grey content area containing your HomeScreen component. The styles you see for the navigation bar and the content area are the default configuration for a StackNavigator, we'll learn how to configure those later.

The casing of the route name doesn't matter -- you can use lowercase home or capitalized Home, it's up to you. We prefer capitalizing our route names.

The only required configuration for a route is the screen component. You can read more about the other options available in the StackNavigator reference.

In React Native, the component exported from App.js is the entry point (or root component) for your app -- it is the component from which every other component descends. It's often useful to have more control over the component at the root of your app than you would get from exporting a StackNavigator, so let's export a component that just renders our StackNavigator.

const RootStack = StackNavigator({
Home: {
screen: HomeScreen,
},
});

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

Adding a second route

The <RootStack /> component doesn't accept any props -- all configuration is specified in the options parameter to the StackNavigator function. We left the options blank, so it just uses the default configuration. To see an example of using the options object, we will add a second screen to the StackNavigator.

// Other code for HomeScreen here...

class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}

const RootStack = StackNavigator(
{
Home: {
screen: HomeScreen,
},
Details: {
screen: DetailsScreen,
},
},
{