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
Summary:
Docs change to encourage people to use React Navigation over other options.
Explains a bit of history about Navigator and NavigationExperimental.
Remove an intro guide that encourages use of Navigator. Hopefully the existing ReactNav guide fills this void.
Navigator docs are less emphasized but still present.
Reviewed By: mkonicek
Differential Revision: D4634452
fbshipit-source-id: 26763c2f02530009b3dfd20b0590fadcb5ea35ee
Copy file name to clipboardExpand all lines: docs/MoreResources.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ layout: docs
5
5
category: The Basics
6
6
permalink: docs/more-resources.html
7
7
next: integration-with-existing-apps
8
-
previous: using-navigators
8
+
previous: networking
9
9
---
10
10
11
11
If you just read through this website, you should be able to build a pretty cool React Native app. But React Native isn't just a product made by one company - it's a community of thousands of developers. So if you're interested in React Native, here's some related stuff you might want to check out.
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`.
11
+
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 React Navigation.
12
+
13
+
If you are only targeting iOS and would like to stick to the native look and feel, check out `NavigatorIOS`. The `Navigator` component is older but has been thoroughly tested in production.
14
+
15
+
## React Navigation
16
+
17
+
The community solution to navigation is a standalone library that allows developers to set up the screens of an app with just a few lines of code.
18
+
19
+
The first step is to install in your app:
20
+
21
+
```
22
+
npm install --save react-navigation
23
+
```
24
+
25
+
Then you can quickly create an app with a home screen and a profile screen:
26
+
27
+
```
28
+
import {
29
+
StackNavigator,
30
+
} from 'react-navigation';
31
+
32
+
const App = StackNavigator({
33
+
Main: {screen: MainScreen},
34
+
Profile: {screen: ProfileScreen},
35
+
});
36
+
```
37
+
38
+
Each screen component can set navigation options such as the header title. It can use action creators on the `navigation` prop to link to other screens:
39
+
40
+
```
41
+
class MainScreen extends React.Component {
42
+
static navigationOptions = {
43
+
title: 'Welcome',
44
+
};
45
+
render() {
46
+
const { navigate } = this.props.navigation;
47
+
return (
48
+
<Button
49
+
title="Go to Jane's profile"
50
+
onPress={() =>
51
+
navigate('Profile', { name: 'Jane' });
52
+
}
53
+
/>
54
+
);
55
+
}
56
+
}
57
+
```
58
+
59
+
React Navigation routers make it easy to override navigation logic or integrate it into redux. Because routers can be nested inside eachother, developers can override navigation logic for one area of the app without making widespread changes.
60
+
61
+
The views in React Navigation use native components and the `Animated` library to deliver 60fps animations that are run on the native thread. Plus, the animations and gestures can be easily customized.
62
+
63
+
For a complete intro to React Navigation, follow the [getting started guide](https://reactnavigation.org/docs/intro/), or browse other docs such as the [intro to navigators](https://reactnavigation.org/docs/navigators/).
12
64
13
65
## Navigator
14
66
15
-
`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/using-navigators.html).
67
+
Like React Navigation, `Navigator` provides a JavaScript implementation of a navigation stack, so it works on both iOS and Android and is easy to customize. Navigator was released alongside React Native in 2015, so it predates the Animated library with native-thread animations.
16
68
17
69

18
70
19
-
`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.
71
+
`Navigator` can 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.
20
72
21
73
Check out the [Navigator API reference](docs/navigator.html) for specific examples that cover each of these scenarios.
22
74
@@ -36,7 +88,7 @@ If you are targeting iOS only, you may also want to consider using [NavigatorIOS
36
88
/>
37
89
```
38
90
39
-
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.
91
+
Like other navigation systems, `NavigatorIOS` uses routes to represent screens, 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.
40
92
41
93
As `NavigatorIOS` leverages native UIKit navigation, it will automatically render a navigation bar with a back button and title.
42
94
@@ -94,252 +146,10 @@ Check out the [`NavigatorIOS` reference docs](docs/navigatorios.html) to learn m
94
146
95
147
## NavigationExperimental
96
148
97
-
`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.
98
-
99
-
```javascript
100
-
<NavigationCardStack
101
-
onNavigateBack={onPopRouteFunc}
102
-
navigationState={myNavigationState}
103
-
renderScene={renderSceneFun}
104
-
/>
105
-
```
106
-
107
-
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.
120
-
121
-
### Step 1. Define Initial State and Top Level Component
122
-
123
-
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:
124
-
125
-
```javascript
126
-
classBleedingEdgeApplicationextendsComponent {
127
-
constructor(props, context) {
128
-
super(props, context);
129
-
130
-
this.state= {
131
-
// This defines the initial navigation state.
132
-
navigationState: {
133
-
index:0, // Starts with first route focused.
134
-
routes: [{key:'My Initial Scene'}], // Starts with only one route.
<Text>This is a placeholder. We will come back to this and render our navigation here later.</Text>
149
-
);
150
-
}
151
-
}
152
-
```
153
-
154
-
Alright, now we have a simple stateful component that doesn't do much at all. We can change that. Our initial state contains one route, and the current index. That looks suspiciously just like our initial route definition in Navigator. Do you remember which actions its navigator object provided?
155
-
156
-
Push and pop, of course. That seems pretty straightforward to implement. I promised you earlier we would be using reducers to manage state at the top-level object. Sit tight.
157
-
158
-
### Step 2. Reducing the Navigation State
159
-
160
-
NavigationExperimental comes built-in with some useful reducers, and they are all available as part of NavigationStateUtils. The two we will be using right now are called -- yep -- push and pop. They take a navigationState object, and return a new navigationState object.
161
-
162
-
We can use them to write our `_onNavigationChange` function which, given a "push" or "pop" action, will reduce the state accordingly.
163
-
164
-
```javascript
165
-
_onNavigationChange(type) {
166
-
// Extract the navigationState from the current state:
167
-
let {navigationState} =this.state;
168
-
169
-
switch (type) {
170
-
case'push':
171
-
// Push a new route, which in our case is an object with a key value.
172
-
// I am fond of cryptic keys (but seriously, keys should be unique)
173
-
constroute= {key:'Route-'+Date.now()};
174
-
175
-
// Use the push reducer provided by NavigationStateUtils
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.
197
-
198
-
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).
199
-
200
-
### Step 3. Define Scenes
201
-
202
-
First I want to define a Row component out of convenience. It displays some text and can call some function when pressed.
203
-
204
-
```javascript
205
-
classTappableRowextendsComponent {
206
-
render() {
207
-
return (
208
-
<TouchableHighlight
209
-
style={styles.row}
210
-
underlayColor="#D0D0D0"
211
-
onPress={this.props.onPress}>
212
-
<Text style={styles.buttonText}>
213
-
{this.props.text}
214
-
</Text>
215
-
</TouchableHighlight>
216
-
);
217
-
}
218
-
}
219
-
```
220
-
221
-
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.
222
-
223
-
```javascript
224
-
classMyVeryComplexSceneextendsComponent {
225
-
render() {
226
-
return (
227
-
<ScrollView style={styles.scrollView}>
228
-
<Text style={styles.row}>
229
-
Route: {this.props.route.key}
230
-
</Text>
231
-
<TappableRow
232
-
text="Tap me to load the next scene"
233
-
onPress={this.props.onPushRoute}
234
-
/>
235
-
<TappableRow
236
-
text="Tap me to go back"
237
-
onPress={this.props.onPopRoute}
238
-
/>
239
-
</ScrollView>
240
-
);
241
-
}
242
-
}
243
-
```
244
-
245
-
### Step 4. Create a Navigation Stack
246
-
247
-
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.
248
-
249
-
```javascript
250
-
classMyVerySimpleNavigatorextendsComponent {
251
-
252
-
// This sets up the methods (e.g. Pop, Push) for navigation.
// Now we finally get to use the `NavigationCardStack` to render the scenes.
263
-
render() {
264
-
return (
265
-
<NavigationCardStack
266
-
onNavigateBack={this._onPopRoute}
267
-
navigationState={this.props.navigationState}
268
-
renderScene={this._renderScene}
269
-
style={styles.navigator}
270
-
/>
271
-
);
272
-
}
273
-
274
-
// Render a scene for route.
275
-
// The detailed spec of `sceneProps` is defined at `NavigationTypeDefinition`
276
-
// as type `NavigationSceneRendererProps`.
277
-
// Here you could choose to render a different component for each route, but
278
-
// we'll keep it simple.
279
-
_renderScene(sceneProps) {
280
-
return (
281
-
<MyVeryComplexScene
282
-
route={sceneProps.scene.route}
283
-
onPushRoute={this._onPushRoute}
284
-
onPopRoute={this._onPopRoute}
285
-
onExit={this.props.onExit}
286
-
/>
287
-
);
288
-
}
289
-
}
290
-
```
291
-
292
-
That's it -- so close to the finish line I can smell it. Let's plug our new navigator into our top-level component:
293
-
294
-
```javascript
295
-
classBleedingEdgeApplicationextendsComponent {
296
-
297
-
// constructor and other methods omitted for clarity
298
-
299
-
render() {
300
-
return (
301
-
<MyVerySimpleNavigator
302
-
navigationState={this.state.navigationState}
303
-
onNavigationChange={this._onNavigationChange}
304
-
onExit={this._exit}
305
-
/>
306
-
);
307
-
}
308
-
}
309
-
```
310
-
311
-
We're done! Bask in the glory of NavigationExperimental.
312
-
313
-
#### Hey -- I think you are missing something.
149
+
Since early 2016, React Native has shipped with an experimental re-implementation of the original `Navigator` component called `CardStack`. The major benefit it had over `Navigator` is the smooth native-thread animations provided by the Animated library.
314
150
315
-
(Oh yes, sorry about that -- here's our missing imports and styles.)
Because `NavigationExperimental` only included view components, it required a lot of boilerplate to use by itself. Several libraries sprung up around it, making it easier to use. Libraries such as `react-native-router-flux`, `ex-navigation`, and `react-router-native` all wrapped NavigationExperimental views in an easier-to-use API. Authors of many of these libraries now support React Navigation.
342
152
343
-
### Homework
153
+
The `CardStack` and other NavigationExperimental views live on as a part of the React Navigation project. The new library aims to be easy to use, while continuing to enable the smooth and customizable animations that NavigationExperimental pioneered.
344
154
345
-
You are now an expert navigator. Take a look at [NavigationExperimental in UIExplorer](https://github.com/facebook/react-native/tree/master/Examples/UIExplorer/js/NavigationExperimental) to learn how to implement other types of navigation hierarchies, such as a tabbed application with multiple navigation stacks.
155
+
As of React Native 0.43, `NavigationExperimental` is deprecated. It will be removed from the codebase in a later version.
0 commit comments