# React Navigation 1.x Documentation ## Getting started Source: https://reactnavigation.org/docs/1.x/getting-started React Navigation is born from the React Native community's need for an extensible yet easy-to-use navigation solution written entirely in JavaScript (so you can read and understand all of the source), on top of powerful native primitives. Before you commit to using React Navigation for your project, you might want to read the [anti-pitch](pitch.md) — it will help you to understand the tradeoffs that we have chosen along with the areas where we consider the library to be deficient currently. ## What to expect If you're already familiar with React Native then you'll be able to get moving with React Navigation quickly! If not, you may want to read sections 1 to 4 (inclusive) of [React Native Express](http://reactnativeexpress.com/) first, then come back here when you're done. What follows within the _Fundamentals_ section of this documentation is a tour of the most important aspects of React Navigation. It should be enough for you know how to build your typical small mobile application, and give you the background that you need to dive deeper into the more advanced parts of React Navigation. ## Installation Install the `react-navigation` package in your React Native project. ```bash npm2yarn npm install react-navigation ``` You're good to go! Continue to ["Hello React Navigation"](hello-react-navigation.md) to start writing some code. --- ## Hello React Navigation Source: https://reactnavigation.org/docs/1.x/hello-react-navigation In a web browser, you can link to different pages using an anchor (``) 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. ```javascript // 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 ( Home Screen ); } } 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](stack-navigator.md). 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`. ```js const RootStack = StackNavigator({ Home: { screen: HomeScreen, }, }); export default class App extends React.Component { render() { return ; } } ``` ## Adding a second route The `` 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`. ```js // Other code for HomeScreen here... class DetailsScreen extends React.Component { render() { return ( Details Screen ); } } const RootStack = StackNavigator( { Home: { screen: HomeScreen, }, Details: { screen: DetailsScreen, }, }, { initialRouteName: 'Home', } ); // Other code for App component here... ``` Now our stack has two _routes_, a `Home` route and a `Details` route. The `Home` route corresponds to the `HomeScreen` component, and the `Details` route corresponds to the `DetailsScreen` component. The initial route for the stack is the `Home` route. The natural question at this point is: "how do I move from the Home route to the Details route?". That is covered in the next section. ## Summary - React Native doesn't have a built-in API for navigation like a web browser does. React Navigation provides this for you, along with the iOS and Android gestures and animations to transition between screens. - `StackNavigator` is a function that takes a route configuration object and an options object and returns a React component. - The keys in the route configuration object are the route names and the values are the configuration for that route. The only required property on the configuration is the `screen` (the component to use for the route). - To specify what the initial route in a stack is, provide an `initialRouteName` on the stack options object. - [Full source of what we have built so far](https://snack.expo.io/@react-navigation/hello-react-navigation). --- ## Supported React Native versions Source: https://reactnavigation.org/docs/1.x/supported-react-native-versions Currently at the time of writing, `react-navigation@1.x` will work on most of previous `react-native` versions. > Please note that the statement above may not be correct for a particular `react-native` version. If you notice a version that is not working properly, feel free to either file an [issue](https://github.com/react-navigation/react-navigation.github.io/issues/new) or correct it in this page. --- ## Moving between screens Source: https://reactnavigation.org/docs/1.x/navigating In the previous section, ["Hello React Navigation"](hello-react-navigation.md), we defined a `StackNavigator` with two routes (`Home` and `Details`), but we didn't learn how to let a user navigate from `Home` to `Details` (although we did learn how to change the _initial_ route in our code, but forcing our users to clone our repository and change the route in our code in order to see another screen is arguably among the worst user experiences one could imagine). If this was a web browser, we'd be able to write something like this: ``` Go to Details ``` Another way to write this would be: ``` { document.location.href = "details.html"; }}>Go to Details ``` We'll do something similar to the latter, but rather than using a `document` global we'll use the `navigation` prop that is passed down to our screen components. ## Navigating to a new screen ```js import React from 'react'; import { Button, View, Text } from 'react-native'; import { StackNavigator } from 'react-navigation'; class HomeScreen extends React.Component { render() { return ( Home Screen