You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mobile apps rarely consist of just one scene (another word for screen). As soon as you add a second scene to your app, you will have to take into consideration how the user will navigate from one scene to the other.
11
-
12
-
You can use navigators to transition between multiple scenes. These transitions can be typical side-to-side animations down a master/detail stack, or vertical modal popups.
10
+
This guide covers the various navigation components available in React Native. If you are just getting started with navigation, you will probably want to use `Navigator`. If you are only targeting iOS and would like to stick to the native look and feel, check out `NavigatorIOS`. If you are looking for greater control over your navigation stack, you can't go wrong with `NavigationExperimental`.
13
11
14
12
## Navigator
15
13
16
-
React Native has several built-in navigation components, but for your first app you will probably want to use `Navigator`. It provides a JavaScript implementation of a navigation stack, so it works on both iOS and Android and is easy to customize.
14
+
`Navigator`provides a JavaScript implementation of a navigation stack, so it works on both iOS and Android and is easy to customize. This is the same component you used to build your first navigation stack in the [navigators tutorial](docs/navigators.html).
17
15
18
16

19
17
20
-
Something you will encounter a lot when dealing with navigation is the concept of routes. A route is an object that contains information about a scene. It is used to provide all the context that the navigator's `renderScene` function needs to render a scene. A basic `Navigator` implementation may look like this:
The above example will display a single scene, but in order to push a new scene onto screen you will need to learn about `push` and `pop`. These two methods are provided by the `navigator` object that is passed to your `renderScene` function. They can be used, as you may have realized, to push and pop routes into your navigation stack.
32
-
33
-
A more complete example that demonstrates the pushing and popping of routes could therefore look something like this:
18
+
`Navigator` can easily be adapted to render different components based on the current route in its `renderScene` function. It will transition new scenes onto the screen by sliding in from the right by default, but you can control this behavior by using the `configureScene` function. You can also configure a navigation bar through the `navigationBar` prop.
In this example, the `MyScene` component is passed the title of the current route via the `title` prop. It displays two tappable components that call the `onForward` and `onBack` functions passed through its props, which in turn will call `navigator.push()` and `navigator.pop()` as needed.
93
-
94
-
While this is a very basic example, it can easily be adapted to render an entirely different component based on the route that is passed to the `renderScene` function. Navigator will push new scenes from the right by default, and you can control this behavior by using the `configureScene` function. You can also configure a navigation bar through the `navigationBar` prop.
95
-
96
-
Check out the [Navigator API reference](docs/navigator.html) for more code samples.
20
+
Check out the [Navigator API reference](docs/navigator.html) for specific examples that cover each of these scenarios.
97
21
98
22
## NavigatorIOS
99
23
100
24
If you are targeting iOS only, you may also want to consider using [NavigatorIOS](docs/navigatorios.html). It looks and feels just like [`UINavigationController`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/), because it is actually built on top of it.
101
25
102
26

103
27
104
-
```js
28
+
```javascript
105
29
<NavigatorIOS
106
30
initialRoute={{
107
31
component: MyScene,
@@ -111,13 +35,11 @@ If you are targeting iOS only, you may also want to consider using [NavigatorIOS
111
35
/>
112
36
```
113
37
114
-
Just like Navigator, NavigatorIOS uses routes to represent scenes, with some important differences. The actual component that will be rendered can be specified using the `component` key in the route, and any props that should be passed to this component can be specified in `passProps`. A "navigator" object is automatically passed as a prop to the component, allowing you to call `push` and `pop` as needed.
38
+
Just like `Navigator`, `NavigatorIOS` uses routes to represent scenes, with some important differences. The actual component that will be rendered can be specified using the `component` key in the route, and any props that should be passed to this component can be specified in `passProps`. A "navigator" object is automatically passed as a prop to the component, allowing you to call `push` and `pop` as needed.
115
39
116
-
As NavigatorIOS leverages native UIKit navigation, it will automatically render a navigation bar with a back button and title.
40
+
As `NavigatorIOS` leverages native UIKit navigation, it will automatically render a navigation bar with a back button and title.
117
41
118
-
Check out the [NavigatorIOS reference docs](docs/navigatorios.html) to learn more about this component.
@@ -165,23 +87,25 @@ class MyScene extends Component {
165
87
}
166
88
```
167
89
90
+
Check out the [`NavigatorIOS` reference docs](docs/navigatorios.html) to learn more about this component.
91
+
168
92
> You may also want to check out [react-native-navigation](https://github.com/wix/react-native-navigation), a component that aims to provide native navigation on both iOS and Android.
169
93
170
94
## NavigationExperimental
171
95
172
-
Navigator and NavigatorIOS are both stateful components. If your app has multiple of these, it can become tricky to coordinate navigation transitions between them. NavigationExperimental provides a different approach to navigation, allowing any view to act as a navigation view and using reducers to manipulate state at a top-level object. It is bleeding edge as the name implies, but you might want to check it out if you are craving greater control over your app's navigation.
96
+
`Navigator` and `NavigatorIOS` are both stateful components. If your app has multiple of these, it can become tricky to coordinate navigation transitions between them. NavigationExperimental provides a different approach to navigation, allowing any view to act as a navigation view and using reducers to manipulate state at a top-level object. It is bleeding edge as the name implies, but you might want to check it out if you are craving greater control over your app's navigation.
173
97
174
-
```js
98
+
```javascript
175
99
<NavigationCardStack
176
100
onNavigateBack={onPopRouteFunc}
177
101
navigationState={myNavigationState}
178
102
renderScene={renderSceneFun}
179
103
/>
180
104
```
181
105
182
-
You can import NavigationExperimental like any other component in React Native. Once you have that, you can deconstruct any additional components from NavigationExperimental that you may find useful. Since I am feeling like building navigation stacks today, I'll go ahead and pick out NavigationCardStack and NavigationStateUtils.
106
+
You can import `NavigationExperimental` like any other component in React Native. Once you have that, you can deconstruct any additional components from `NavigationExperimental` that you may find useful. Since I am feeling like building navigation stacks today, I'll go ahead and pick out `NavigationCardStack` and `NavigationStateUtils`.
As I said earlier, NavigationExperimental takes a different approach than Navigator and NavigatorIOS. Using it to build a navigation stack requires a few more steps than the stateful components, but the payoff is worth it.
118
+
As I said earlier, `NavigationExperimental` takes a different approach than `Navigator` and `NavigatorIOS`. Using it to build a navigation stack requires a few more steps than the stateful components, but the payoff is worth it.
195
119
196
120
### Step 1. Define Initial State and Top Level Component
197
121
198
122
Create a new component for your application. This will be the top-level object, so we will define the initial state here. The navigation state will be defined in the `navigationState` key, where we define our initial route:
199
123
200
-
```js
124
+
```javascript
201
125
classBleedingEdgeApplicationextendsComponent {
202
126
constructor(props, context) {
203
127
super(props, context);
@@ -236,7 +160,7 @@ NavigationExperimental comes built-in with a some useful reducers, and they are
236
160
237
161
We can use them to write our `_onNavigationChange` function which, given a "push" or "pop" action, will reduce the state accordingly.
238
162
239
-
```js
163
+
```javascript
240
164
_onNavigationChange(type) {
241
165
// Extract the navigationState from the current state:
242
166
let {navigationState} =this.state;
@@ -270,13 +194,13 @@ _onNavigationChange(type) {
270
194
271
195
Cool. I'm getting the hang of this. This is the heart of NavigationExperimental. We are only handling two actions here, but a more complex application could also take into account a "back" action (e.g. Android back button), as well as handle the transition between several tabs in a tabbed application.
272
196
273
-
I am still missing the initial scene that will be rendered (as well as the actual navigator that will wrap it, but lets not get ahead of ourselves).
197
+
I am still missing the initial scene that will be rendered (as well as the actual navigator that will wrap it, but let's not get ahead of ourselves).
274
198
275
199
### Step 3. Define Scenes
276
200
277
201
First I want to define a Row component out of convenience. It displays some text and can call some function when pressed.
278
202
279
-
```js
203
+
```javascript
280
204
classTappableRowextendsComponent {
281
205
render() {
282
206
return (
@@ -295,7 +219,7 @@ class TappableRow extends Component {
295
219
296
220
Now I will define my actual scene. It uses a scroll view to display a vertical list of items. The first row displays the current route's key, and two more rows will call our theoretical navigator's push and pop functions.
297
221
298
-
```js
222
+
```javascript
299
223
classMyVeryComplexSceneextendsComponent {
300
224
render() {
301
225
return (
@@ -321,7 +245,7 @@ class MyVeryComplexScene extends Component {
321
245
322
246
Now that I have defined the state and a function to manage it, I think I can go ahead and create a proper navigator component now. While I'm at it, I'll render my scene after configuring it with the current route's props.
323
247
324
-
```js
248
+
```javascript
325
249
classMyVerySimpleNavigatorextendsComponent {
326
250
327
251
// This sets up the methods (e.g. Pop, Push) for navigation.
@@ -364,9 +288,9 @@ class MyVerySimpleNavigator extends Component {
364
288
}
365
289
```
366
290
367
-
That's it -- so close to the finish line I can smell it. Lets plug our new navigator into our top-level component:
291
+
That's it -- so close to the finish line I can smell it. Let's plug our new navigator into our top-level component:
368
292
369
-
```js
293
+
```javascript
370
294
classBleedingEdgeApplicationextendsComponent {
371
295
372
296
// constructor and other methods omitted for clarity
@@ -389,7 +313,7 @@ We're done! Bask in the glory of NavigationExperimental.
389
313
390
314
(Oh yes, sorry about that -- here's our missing imports and styles.)
0 commit comments