|
| 1 | +# Hello Mobile Navigation |
| 2 | + |
| 3 | +Let's use React Navigation to build a simple chat-like application for Android and iOS. |
| 4 | + |
| 5 | +## Setup and Installation |
| 6 | + |
| 7 | +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`: |
| 8 | + |
| 9 | +```sh |
| 10 | +# Create a new React Native App |
| 11 | +react-native init SimpleApp |
| 12 | +cd SimpleApp |
| 13 | + |
| 14 | +# Install the latest version of react-navigation from npm |
| 15 | +npm install --save react-navigation |
| 16 | + |
| 17 | +# Run the new app |
| 18 | +react-native run-android |
| 19 | +# or: |
| 20 | +react-native run-ios |
| 21 | +``` |
| 22 | + |
| 23 | +If you are using `create-react-native-app` instead of `react-native init`, then: |
| 24 | + |
| 25 | +```sh |
| 26 | +# Create a new React Native App |
| 27 | +create-react-native-app SimpleApp |
| 28 | +cd SimpleApp |
| 29 | + |
| 30 | +# Install the latest version of react-navigation from npm |
| 31 | +npm install --save react-navigation |
| 32 | + |
| 33 | +# Run the new app |
| 34 | +npm start |
| 35 | + |
| 36 | +# This will start a development server for you and print a QR code in your terminal. |
| 37 | +``` |
| 38 | + |
| 39 | +Verify that you can successfully see the bare sample app run on iOS and/or Android: |
| 40 | + |
| 41 | +```phone-example |
| 42 | +bare-project |
| 43 | +``` |
| 44 | + |
| 45 | +We want to share code on iOS and Android, so let's delete the contents of `index.js` (or `index.ios.js` and `index.android.js` if using a React Native version before 0.49) and replace it with `import './App';` - after which, we need to create the new file for our app implementation, `App.js` (if you used `create-react-native-app` this has been already done) |
| 46 | + |
| 47 | +## Introducing Stack Navigator |
| 48 | + |
| 49 | +For our app, we want to use the `StackNavigator` because conceptually we want to obtain a 'card stack' effect of movement, 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: |
| 50 | + |
| 51 | +```js |
| 52 | +import React from 'react'; |
| 53 | +import { |
| 54 | + AppRegistry, |
| 55 | + Text, |
| 56 | +} from 'react-native'; |
| 57 | +import { StackNavigator } from 'react-navigation'; |
| 58 | + |
| 59 | +class HomeScreen extends React.Component { |
| 60 | + static navigationOptions = { |
| 61 | + title: 'Welcome', |
| 62 | + }; |
| 63 | + render() { |
| 64 | + return <Text>Hello, Navigation!</Text>; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export const SimpleApp = StackNavigator({ |
| 69 | + Home: { screen: HomeScreen }, |
| 70 | +}); |
| 71 | + |
| 72 | +AppRegistry.registerComponent('SimpleApp', () => SimpleApp); |
| 73 | +``` |
| 74 | + |
| 75 | +If you used `create-react-native-app` the already existing `App.js` will be modified to |
| 76 | + |
| 77 | +```js |
| 78 | +import React from 'react'; |
| 79 | +import { StyleSheet, Text, View } from 'react-native'; |
| 80 | +import { StackNavigator } from 'react-navigation'; |
| 81 | + |
| 82 | +class HomeScreen extends React.Component { |
| 83 | + static navigationOptions = { |
| 84 | + title: 'Welcome' |
| 85 | + }; |
| 86 | + render() { |
| 87 | + return <Text>Hello, Navigation!</Text>; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +const SimpleApp = StackNavigator({ |
| 92 | + Home: { screen: HomeScreen } |
| 93 | +}); |
| 94 | + |
| 95 | +export default class App extends React.Component { |
| 96 | + render() { |
| 97 | + return <SimpleApp />; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +const styles = StyleSheet.create({ |
| 102 | + container: { |
| 103 | + flex: 1, |
| 104 | + backgroundColor: '#fff', |
| 105 | + alignItems: 'center', |
| 106 | + justifyContent: 'center' |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +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. |
| 113 | + |
| 114 | +Now the same screen should appear on both iPhone and Android apps: |
| 115 | + |
| 116 | +```phone-example |
| 117 | +first-screen |
| 118 | +``` |
| 119 | + |
| 120 | +## Adding a New Screen |
| 121 | + |
| 122 | +In our `App.js` file, let's add a new screen called `ChatScreen`, defining it under `HomeScreen`: |
| 123 | + |
| 124 | +```js |
| 125 | +// ... |
| 126 | + |
| 127 | +class HomeScreen extends React.Component { |
| 128 | + //... |
| 129 | +} |
| 130 | + |
| 131 | +class ChatScreen extends React.Component { |
| 132 | + static navigationOptions = { |
| 133 | + title: 'Chat with Lucy', |
| 134 | + }; |
| 135 | + render() { |
| 136 | + return ( |
| 137 | + <View> |
| 138 | + <Text>Chat with Lucy</Text> |
| 139 | + </View> |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +``` |
| 145 | + |
| 146 | +We can then add a button to our `HomeScreen` component that links to `ChatScreen`: we need to use the provided method `navigate` (from the [screen navigation prop](/docs/navigators/navigation-prop)) by giving it the `routeName` of the screen we want to reach, in this case `Chat`. |
| 147 | + |
| 148 | +```js |
| 149 | +class HomeScreen extends React.Component { |
| 150 | + static navigationOptions = { |
| 151 | + title: 'Welcome', |
| 152 | + }; |
| 153 | + render() { |
| 154 | + const { navigate } = this.props.navigation; |
| 155 | + return ( |
| 156 | + <View> |
| 157 | + <Text>Hello, Chat App!</Text> |
| 158 | + <Button |
| 159 | + onPress={() => navigate('Chat')} |
| 160 | + title="Chat with Lucy" |
| 161 | + /> |
| 162 | + </View> |
| 163 | + ); |
| 164 | + } |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +(*don't forget to import View and Button from react-native:* `import { AppRegistry, Text, View, Button } from 'react-native';`) |
| 169 | + |
| 170 | +But that won't work until we say to our `StackNavigator` of the existence of the `Chat` screen, like so: |
| 171 | + |
| 172 | +```js |
| 173 | +export const SimpleApp = StackNavigator({ |
| 174 | + Home: { screen: HomeScreen }, |
| 175 | + Chat: { screen: ChatScreen }, |
| 176 | +}); |
| 177 | +``` |
| 178 | + |
| 179 | +Now you can navigate to your new screen, and go back: |
| 180 | + |
| 181 | +```phone-example |
| 182 | +first-navigation |
| 183 | +``` |
| 184 | + |
| 185 | +## Passing params |
| 186 | + |
| 187 | +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. |
| 188 | + |
| 189 | +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. |
| 190 | + |
| 191 | +```js |
| 192 | +class HomeScreen extends React.Component { |
| 193 | + static navigationOptions = { |
| 194 | + title: 'Welcome', |
| 195 | + }; |
| 196 | + render() { |
| 197 | + const { navigate } = this.props.navigation; |
| 198 | + return ( |
| 199 | + <View> |
| 200 | + <Text>Hello, Chat App!</Text> |
| 201 | + <Button |
| 202 | + onPress={() => navigate('Chat', { user: 'Lucy' })} |
| 203 | + title="Chat with Lucy" |
| 204 | + /> |
| 205 | + </View> |
| 206 | + ); |
| 207 | + } |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +We can then edit our `ChatScreen` component to display the `user` param that was passed in through the route: |
| 212 | + |
| 213 | +```js |
| 214 | +class ChatScreen extends React.Component { |
| 215 | + // Nav options can be defined as a function of the screen's props: |
| 216 | + static navigationOptions = ({ navigation }) => ({ |
| 217 | + title: `Chat with ${navigation.state.params.user}`, |
| 218 | + }); |
| 219 | + render() { |
| 220 | + // The screen's current route is passed in to `props.navigation.state`: |
| 221 | + const { params } = this.props.navigation.state; |
| 222 | + return ( |
| 223 | + <View> |
| 224 | + <Text>Chat with {params.user}</Text> |
| 225 | + </View> |
| 226 | + ); |
| 227 | + } |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +Now you can see the name when you navigate to the Chat screen. Try changing the `user` param in `HomeScreen` and see what happens! |
| 232 | + |
| 233 | +```phone-example |
| 234 | +first-navigation |
| 235 | +``` |
0 commit comments