2017-01-26 11:49:39 -08:00
# Hello Mobile Navigation
Let's use React Navigation to build a simple chat application for Android and iOS.
## Setup and Installation
First, make sure you're [all set up to use React Native ](http://facebook.github.io/react-native/docs/getting-started.html ). Next, create a new project and add `react-navigation` :
```sh
# Create a new React Native App
react-native init SimpleApp
cd SimpleApp
# Install the latest version of react-navigation from npm
npm install --save react-navigation
# Run the new app
react-native run-android # or:
react-native run-ios
```
2017-08-12 12:58:27 +07:00
If you are using `create-react-native-app` instead of `react-native init` , then:
```sh
# Create a new React Native App
create-react-native-app SimpleApp
cd SimpleApp
# Install the latest version of react-navigation from npm
npm install --save react-navigation
# Run the new app
npm start
2017-08-29 17:25:11 +07:00
# This will start a development server for you and print a QR code in your terminal.
2017-09-15 16:01:26 -05:00
```
2017-08-12 12:58:27 +07:00
2017-01-26 11:49:39 -08:00
Verify that you can successfully see the bare sample app run on iOS and/or Android:
```phone-example
bare-project
```
We want to share code on iOS and Android, so lets delete the contents of `index.ios.js` and `index.android.js` and replace it with `import './App';` .
Now lets create the new file for our app implementation, `App.js` .
## Introducing Stack Navigator
2017-02-20 13:02:34 -05:00
For our app, we want to use the `StackNavigator` because we want a conceptual 'stack' navigation, where each new screen is put on the top of the stack and going back removes a screen from the top of the stack. Let's start with just one screen:
2017-01-26 11:49:39 -08:00
```js
import React from 'react';
import {
AppRegistry,
Text,
} from 'react-native';
import { StackNavigator } from 'react-navigation';
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
2017-01-27 16:56:19 +01:00
return < Text > Hello, Navigation!< / Text > ;
2017-01-26 11:49:39 -08:00
}
}
2017-09-15 16:01:26 -05:00
export default const SimpleApp = StackNavigator({
2017-01-26 11:49:39 -08:00
Home: { screen: HomeScreen },
});
2017-08-29 17:25:11 +07:00
// if you are using create-react-native-app you don't need this line
2017-01-26 11:49:39 -08:00
AppRegistry.registerComponent('SimpleApp', () => SimpleApp);
```
The `title` of the screen is configurable on the [static `navigationOptions` ](/docs/navigators/navigation-options ), where many options can be set to configure the presentation of the screen in the navigator.
Now the same screen should appear on both iPhone and Android apps:
```phone-example
first-screen
```
## Adding a New Screen
2017-02-20 13:02:34 -05:00
In our `App.js` file, let's add a new screen called `ChatScreen` :
```js
class ChatScreen extends React.Component {
static navigationOptions = {
title: 'Chat with Lucy',
};
render() {
return (
< View >
< Text > Chat with Lucy< / Text >
< / View >
);
}
}
```
We can then add a button to our `HomeScreen` component that links to `ChatScreen` using the `routeName` `Chat` .
2017-01-26 11:49:39 -08:00
```js
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
< View >
< Text > Hello, Chat App!< / Text >
< Button
2017-02-20 13:02:34 -05:00
onPress={() => navigate('Chat')}
2017-01-26 11:49:39 -08:00
title="Chat with Lucy"
/>
< / View >
);
}
}
```
2017-02-20 13:02:34 -05:00
We're using the navigate function from the [screen navigation prop ](/docs/navigators/navigation-prop ) to go to `ChatScreen` . But that won't work until we add this to our `StackNavigator` like so:
```js
2017-09-15 16:01:26 -05:00
export default const SimpleApp = StackNavigator({
2017-02-20 13:02:34 -05:00
Home: { screen: HomeScreen },
Chat: { screen: ChatScreen },
});
```
Now you can navigate to your new screen, and go back:
```phone-example
first-navigation
```
## Passing params
2017-01-26 11:49:39 -08:00
2017-02-20 13:02:34 -05:00
Hardcoding a name into the `ChatScreen` isn't ideal. It'd be more useful if we could pass a name to be rendered instead, so let's do that.
2017-04-05 01:00:37 -03:00
In addition to specifying the target `routeName` in the navigate function, we can pass params that will be put into the new route. First, we'll edit our `HomeScreen` component to pass a `user` param into the route.
2017-02-20 13:02:34 -05:00
```js
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
< View >
< Text > Hello, Chat App!< / Text >
< Button
onPress={() => navigate('Chat', { user: 'Lucy' })}
title="Chat with Lucy"
/>
< / View >
);
}
}
```
2017-04-05 01:00:37 -03:00
We can then edit our `ChatScreen` component to display the `user` param that was passed in through the route:
2017-01-26 11:49:39 -08:00
```js
class ChatScreen extends React.Component {
2017-04-13 00:49:08 +02:00
// Nav options can be defined as a function of the screen's props:
static navigationOptions = ({ navigation }) => ({
title: `Chat with ${navigation.state.params.user}` ,
});
2017-01-26 11:49:39 -08:00
render() {
// The screen's current route is passed in to `props.navigation.state` :
const { params } = this.props.navigation.state;
return (
< View >
< Text > Chat with {params.user}< / Text >
< / View >
);
}
}
```
2017-04-05 01:00:37 -03:00
Now you can see the name when you navigate to the Chat screen. Try changing the `user` param in `HomeScreen` and see what happens!
2017-01-26 11:49:39 -08:00
```phone-example
first-navigation
```