Skip to content

Commit 2bad8be

Browse files
Christine AbernathyMorgan Pretty
authored andcommitted
Update Navigator component doc
Summary: Related to facebook#8203 to update the Navigator component reference doc. **Test plan (required)** Started up the website and checked: http://localhost:8079/react-native/docs/navigator.html ![component_navigator_2](https://cloud.githubusercontent.com/assets/691109/16280426/3f2cdc32-3874-11e6-810b-ca34d7bd4972.png) **Note** The code is not Flow-ified so depended on jsdoc formatting to get the method parameter types. There's a current issue with handling optional types via react-docgen which parses components. There's an open PR to look into this: reactjs/react-docgen#89. When that's resolved the `replaceAtIndex` method parameter type that's documented for `cb` needs to be updated to make it optional. Closes facebook#8318 Differential Revision: D3471185 Pulled By: JoelMarcey fbshipit-source-id: 99f85ee2ab00dc200cf2812cce5b3ccec743d6a0
1 parent 6cd356a commit 2bad8be

File tree

1 file changed

+186
-56
lines changed

1 file changed

+186
-56
lines changed

Libraries/CustomComponents/Navigator/Navigator.js

Lines changed: 186 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -129,54 +129,168 @@ var GESTURE_ACTIONS = [
129129
];
130130

131131
/**
132-
* Use `Navigator` to transition between different scenes in your app. To
133-
* accomplish this, provide route objects to the navigator to identify each
134-
* scene, and also a `renderScene` function that the navigator can use to
135-
* render the scene for a given route.
132+
* `Navigator` handles the transition between different scenes in your app.
133+
* You should consider using this component instead of `NavigatorIOS` if you're
134+
* building a cross-platform app. `Navigator` is implemented in JavaScript
135+
* whereas `NavigatorIOS` is a wrapper around `UINavigationController`.
136136
*
137-
* To change the animation or gesture properties of the scene, provide a
138-
* `configureScene` prop to get the config object for a given route. See
139-
* `Navigator.SceneConfigs` for default animations and more info on
140-
* scene config options.
137+
* To set up the `Navigator` you provide one or more objects called routes,
138+
* to identify each scene. You also provide a `renderScene` function that
139+
* renders the scene for each route object.
140+
*
141+
* ```
142+
* import React, { Component } from 'react';
143+
* import { Text, Navigator } from 'react-native';
144+
*
145+
* class NavAllDay extends Component {
146+
* render() {
147+
* return (
148+
* <Navigator
149+
* initialRoute={{name: 'Awesome Scene'}}
150+
* renderScene={(route, navigator) =>
151+
* <Text>Hello {route.name}!</Text>
152+
* }
153+
* style={{padding: 100}}
154+
* />
155+
* );
156+
* }
157+
* }
158+
* ```
159+
*
160+
* In the above example, `initialRoute` is used to specify the first route. It
161+
* contains a `name` property that identifies the route. The `renderScene`
162+
* prop returns a function that displays text based on the route's name.
163+
*
164+
* ### Additional Scenes
141165
*
142-
* ### Basic Usage
166+
* The first example demonstrated one scene. To set up multiple scenes, you pass
167+
* the `initialRouteStack` prop to `Navigator`:
143168
*
144169
* ```
145-
* <Navigator
146-
* initialRoute={{name: 'My First Scene', index: 0}}
147-
* renderScene={(route, navigator) =>
148-
* <MySceneComponent
149-
* name={route.name}
150-
* onForward={() => {
151-
* var nextIndex = route.index + 1;
152-
* navigator.push({
153-
* name: 'Scene ' + nextIndex,
154-
* index: nextIndex,
155-
* });
156-
* }}
157-
* onBack={() => {
158-
* if (route.index > 0) {
170+
* render() {
171+
* const routes = [
172+
* {name: 'First Scene', index: 0},
173+
* {name: 'Second Scene', index: 1},
174+
* ];
175+
* return (
176+
* <Navigator
177+
* initialRoute={routes[0]}
178+
* initialRouteStack={routes}
179+
* renderScene={(route, navigator) =>
180+
* <TouchableHighlight onPress={() => {
181+
* if (route.index === 0) {
182+
* navigator.push(routes[1]);
183+
* } else {
159184
* navigator.pop();
160185
* }
161-
* }}
162-
* />
186+
* }}>
187+
* <Text>Hello {route.name}!</Text>
188+
* </TouchableHighlight>
189+
* }
190+
* style={{padding: 100}}
191+
* />
192+
* );
193+
* }
194+
* ```
195+
*
196+
* In the above example, a `routes` variable is defined with two route objects
197+
* representing two scenes. Each route has an `index` property that is used to
198+
* manage the scene being rendered. The `renderScene` method is changed to
199+
* either push or pop the navigator depending on the current route's index.
200+
* Finally, the `Text` component in the scene is now wrapped in a
201+
* `TouchableHighlight` component to help trigger the navigator transitions.
202+
*
203+
* ### Navigation Bar
204+
*
205+
* You can optionally pass in your own navigation bar by returning a
206+
* `Navigator.NavigationBar` component to the `navigationBar` prop in
207+
* `Navigator`. You can configure the navigation bar properties, through
208+
* the `routeMapper` prop. There you set up the left, right, and title
209+
* properties of the navigation bar:
210+
*
211+
* ```
212+
* <Navigator
213+
* renderScene={(route, navigator) =>
214+
* // ...
215+
* }
216+
* navigationBar={
217+
* <Navigator.NavigationBar
218+
* routeMapper={{
219+
* LeftButton: (route, navigator, index, navState) =>
220+
* { return (<Text>Cancel</Text>); },
221+
* RightButton: (route, navigator, index, navState) =>
222+
* { return (<Text>Done</Text>); },
223+
* Title: (route, navigator, index, navState) =>
224+
* { return (<Text>Awesome Nav Bar</Text>); },
225+
* }}
226+
* style={{backgroundColor: 'gray'}}
227+
* />
228+
* }
229+
* />
230+
* ```
231+
*
232+
* When configuring the left, right, and title items for the navigation bar,
233+
* you have access to info such as the current route object and navigation
234+
* state. This allows you to customize the title for each scene as well as
235+
* the buttons. For example, you can choose to hide the left button for one of
236+
* the scenes.
237+
*
238+
* Typically you want buttons to represent the left and right buttons. Building
239+
* on the previous example, you can set this up as follows:
240+
*
241+
* ```
242+
* LeftButton: (route, navigator, index, navState) =>
243+
* {
244+
* if (route.index === 0) {
245+
* return null;
246+
* } else {
247+
* return (
248+
* <TouchableHighlight onPress={() => navigator.pop()}>
249+
* <Text>Back</Text>
250+
* </TouchableHighlight>
251+
* );
163252
* }
164-
* />
253+
* },
165254
* ```
255+
*
256+
* This sets up a left navigator bar button that's visible on scenes after the
257+
* the first one. When the button is tapped the navigator is popped.
258+
*
259+
* ### Scene Transitions
260+
*
261+
* To change the animation or gesture properties of the scene, provide a
262+
* `configureScene` prop to get the config object for a given route:
263+
*
264+
* ```
265+
* <Navigator
266+
* renderScene={(route, navigator) =>
267+
* // ...
268+
* }
269+
* configureScene={(route, routeStack) =>
270+
* Navigator.SceneConfigs.FloatFromBottom}
271+
* />
272+
* ```
273+
* In the above example, the newly pushed scene will float up from the bottom.
274+
* See `Navigator.SceneConfigs` for default animations and more info on
275+
* available [scene config options](/react-native/docs/navigator.html#configurescene).
166276
*/
167277
var Navigator = React.createClass({
168278

169279
propTypes: {
170280
/**
171-
* Optional function that allows configuration about scene animations and
172-
* gestures. Will be invoked with the route and the routeStack and should
173-
* return a scene configuration object
281+
* Optional function where you can configure scene animations and
282+
* gestures. Will be invoked with `route` and `routeStack` parameters,
283+
* where `route` corresponds to the current scene being rendered by the
284+
* `Navigator` and `routeStack` is the set of currently mounted routes
285+
* that the navigator could transition to.
286+
*
287+
* The function should return a scene configuration object.
174288
*
175289
* ```
176290
* (route, routeStack) => Navigator.SceneConfigs.FloatFromRight
177291
* ```
178292
*
179-
* Available options are:
293+
* Available scene configutation options are:
180294
*
181295
* - Navigator.SceneConfigs.PushFromRight (default)
182296
* - Navigator.SceneConfigs.FloatFromRight
@@ -194,7 +308,7 @@ var Navigator = React.createClass({
194308

195309
/**
196310
* Required function which renders the scene for a given route. Will be
197-
* invoked with the route and the navigator object
311+
* invoked with the `route` and the `navigator` object.
198312
*
199313
* ```
200314
* (route, navigator) =>
@@ -204,45 +318,53 @@ var Navigator = React.createClass({
204318
renderScene: PropTypes.func.isRequired,
205319

206320
/**
207-
* Specify a route to start on. A route is an object that the navigator
208-
* will use to identify each scene to render. `initialRoute` must be
209-
* a route in the `initialRouteStack` if both props are provided. The
210-
* `initialRoute` will default to the last item in the `initialRouteStack`.
321+
* The initial route for navigation. A route is an object that the navigator
322+
* will use to identify each scene it renders.
323+
*
324+
* If both `initialRoute` and `initialRouteStack` props are passed to
325+
* `Navigator`, then `initialRoute` must be in a route in
326+
* `initialRouteStack`. If `initialRouteStack` is passed as a prop but
327+
* `initialRoute` is not, then `initialRoute` will default internally to
328+
* the last item in `initialRouteStack`.
211329
*/
212330
initialRoute: PropTypes.object,
213331

214332
/**
215-
* Provide a set of routes to initially mount. Required if no initialRoute
216-
* is provided. Otherwise, it will default to an array containing only the
217-
* `initialRoute`
333+
* Pass this in to provide a set of routes to initially mount. This prop
334+
* is required if `initialRoute` is not provided to the navigator. If this
335+
* prop is not passed in, it will default internally to an array
336+
* containing only `initialRoute`.
218337
*/
219338
initialRouteStack: PropTypes.arrayOf(PropTypes.object),
220339

221340
/**
222-
* Will emit the target route upon mounting and before each nav transition
341+
* Pass in a function to get notified with the target route when
342+
* the navigator component is mounted and before each navigator transition.
223343
*/
224344
onWillFocus: PropTypes.func,
225345

226346
/**
227347
* Will be called with the new route of each scene after the transition is
228-
* complete or after the initial mounting
348+
* complete or after the initial mounting.
229349
*/
230350
onDidFocus: PropTypes.func,
231351

232352
/**
233-
* Optionally provide a component as navigation bar that persists across scene
234-
* transitions. The component will receive two props: `navigator` and `navState`.
235-
* It will be rerendered when the routes change.
353+
* Use this to provide an optional component representing a navigation bar
354+
* that is persisted across scene transitions. This component will receive
355+
* two props: `navigator` and `navState` representing the navigator
356+
* component and its state. The component is re-rendered when the route
357+
* changes.
236358
*/
237359
navigationBar: PropTypes.node,
238360

239361
/**
240-
* Optionally provide the navigator object from a parent Navigator
362+
* Optionally pass in the navigator object from a parent `Navigator`.
241363
*/
242364
navigator: PropTypes.object,
243365

244366
/**
245-
* Styles to apply to the container of each scene
367+
* Styles to apply to the container of each scene.
246368
*/
247369
sceneStyle: View.propTypes.style,
248370
},
@@ -348,11 +470,10 @@ var Navigator = React.createClass({
348470
/**
349471
* Reset every scene with an array of routes.
350472
*
351-
* @param {RouteStack} nextRouteStack Next route stack to reinitialize. This
352-
* doesn't accept stack item `id`s, which implies that all existing items are
353-
* destroyed, and then potentially recreated according to `routeStack`. Does
354-
* not animate, immediately replaces and rerenders navigation bar and stack
355-
* items.
473+
* @param {RouteStack} nextRouteStack Next route stack to reinitialize.
474+
* All existing route stacks are destroyed an potentially recreated. There
475+
* is no accompanying animation and this method immediately replaces and
476+
* re-renders the navigation bar and the stack items.
356477
*/
357478
immediatelyResetRouteStack: function(nextRouteStack) {
358479
var destIndex = nextRouteStack.length - 1;
@@ -893,7 +1014,9 @@ var Navigator = React.createClass({
8931014
},
8941015

8951016
/**
896-
* Transition to an existing scene without unmounting
1017+
* Transition to an existing scene without unmounting.
1018+
* @param {object} route Route to transition to. The specified route must
1019+
* be in the currently mounted set of routes defined in `routeStack`.
8971020
*/
8981021
jumpTo: function(route) {
8991022
var destIndex = this.state.routeStack.indexOf(route);
@@ -920,7 +1043,8 @@ var Navigator = React.createClass({
9201043

9211044
/**
9221045
* Navigate forward to a new scene, squashing any scenes that you could
923-
* `jumpForward` to.
1046+
* jump forward to.
1047+
* @param {object} route Route to push into the navigator stack.
9241048
*/
9251049
push: function(route) {
9261050
invariant(!!route, 'Must supply route to push');
@@ -984,10 +1108,11 @@ var Navigator = React.createClass({
9841108
},
9851109

9861110
/**
987-
* Replace a scene as specified by an index
988-
*
989-
* `index` specifies the route in the stack that should be replaced.
990-
* If it's negative, it counts from the back.
1111+
* Replace a scene as specified by an index.
1112+
* @param {object} route Route representing the new scene to render.
1113+
* @param {number} index The route in the stack that should be replaced.
1114+
* If negative, it counts from the back of the stack.
1115+
* @param {Function} cb Callback function when the scene has been replaced.
9911116
*/
9921117
replaceAtIndex: function(route, index, cb) {
9931118
invariant(!!route, 'Must supply route to replace');
@@ -1020,13 +1145,15 @@ var Navigator = React.createClass({
10201145

10211146
/**
10221147
* Replace the current scene with a new route.
1148+
* @param {object} route Route that replaces the current scene.
10231149
*/
10241150
replace: function(route) {
10251151
this.replaceAtIndex(route, this.state.presentedIndex);
10261152
},
10271153

10281154
/**
10291155
* Replace the previous scene.
1156+
* @param {object} route Route that replaces the previous scene.
10301157
*/
10311158
replacePrevious: function(route) {
10321159
this.replaceAtIndex(route, this.state.presentedIndex - 1);
@@ -1042,6 +1169,7 @@ var Navigator = React.createClass({
10421169
/**
10431170
* Pop to a particular scene, as specified by its route.
10441171
* All scenes after it will be unmounted.
1172+
* @param {object} route Route to pop to.
10451173
*/
10461174
popToRoute: function(route) {
10471175
var indexOfRoute = this.state.routeStack.indexOf(route);
@@ -1055,6 +1183,7 @@ var Navigator = React.createClass({
10551183

10561184
/**
10571185
* Replace the previous scene and pop to it.
1186+
* @param {object} route Route that replaces the previous scene.
10581187
*/
10591188
replacePreviousAndPop: function(route) {
10601189
if (this.state.routeStack.length < 2) {
@@ -1066,6 +1195,7 @@ var Navigator = React.createClass({
10661195

10671196
/**
10681197
* Navigate to a new scene and reset route stack.
1198+
* @param {object} route Route to navigate to.
10691199
*/
10701200
resetTo: function(route) {
10711201
invariant(!!route, 'Must supply route to push');

0 commit comments

Comments
 (0)