Skip to content

Commit 003dbb8

Browse files
authored
Merge pull request react-navigation#258 from react-navigation/@vonovak/nav-prop-missing-docs
document missing functions on navigation prop
2 parents 1448419 + 08b25d4 commit 003dbb8

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

docs/drawer-actions.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
id: drawer-actions
3+
title: DrawerActions reference
4+
sidebar_label: DrawerActions
5+
---
6+
7+
`DrawerActions` is an object containing methods for generating actions specific to drawer-based navigators. Its methods expand upon the actions available in [NavigationActions](navigation-actions.html).
8+
9+
The following actions are supported:
10+
* [openDrawer](#openDrawer) - open the drawer
11+
* [closeDrawer](#closeDrawer) - close the drawer
12+
* [toggleDrawer](#toggleDrawer) - toggle the state, ie. switche from closed to open and vice versa
13+
14+
### Usage
15+
16+
```js
17+
import { DrawerActions } from 'react-navigation-drawer';
18+
19+
this.props.navigation.dispatch(DrawerActions.toggleDrawer())
20+
```

docs/navigation-prop.md

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Navigation prop reference
44
sidebar_label: Navigation prop
55
---
66

7-
Each `screen` component in your app is provided with the `navigation` prop automatically. It looks like this:
7+
Each `screen` component in your app is provided with the `navigation` prop automatically. The prop contains various convenience functions that dispatch navigation actions on the route's router. It looks like this:
88

99
* `this.props.navigation`
1010
* `navigate` - go to another screen, figures out the action it needs to take to do it
@@ -15,27 +15,44 @@ Each `screen` component in your app is provided with the `navigation` prop autom
1515
* `setParams` - make changes to route's params
1616
* `getParam` - get a specific param with fallback
1717
* `dispatch` - send an action to router
18+
* `dangerouslyGetParent` - function that returns the parent navigator, if any
1819

19-
It's important to highlight the `navigation` prop is _not_ passed in to _all_ components; only `screen` components receive this prop automatically! React Navigation doesn't do anything magic here. For example, if you were to define a `MyBackButton` component and render it as a child of a screen component, you would not be able to access the `navigation` prop on it.
20+
It's important to highlight the `navigation` prop is _not_ passed in to _all_ components; only `screen` components receive this prop automatically! React Navigation doesn't do anything magic here. For example, if you were to define a `MyBackButton` component and render it as a child of a screen component, you would not be able to access the `navigation` prop on it. If, however, you wish to access the `navigation` prop in any of your components, you may use the [`withNavigation`](docs/with-navigation.html) HOC.
2021

21-
There are several additional functions on `this.props.navigation` that only if the current navigator is a stack navigator. These functions are alternatives to `navigate` and `goBack` and you can use whichever you prefer. The functions are:
22+
### Navigator-dependent functions
23+
24+
There are several additional functions present on `this.props.navigation` based on the kind of the current navigator.
25+
26+
If the navigator is a stack navigator, several alternatives to `navigate` and `goBack` are provided and you can use whichever you prefer. The functions are:
2227

2328
* `this.props.navigation`
24-
* `push` - navigate forward to new route in stack
29+
* `push` - push a new route onto the stack
2530
* `pop` - go back in the stack
2631
* `popToTop` - go to the top of the stack
2732
* `replace` - replace the current route with a new one
33+
* `reset` - wipe the navigator state and replace it with the result of several actions
34+
* `dismiss` - dismiss the current stack
35+
36+
37+
If the navigator is a drawer navigator, the following are also available:
38+
39+
* `this.props.navigation`
40+
* `openDrawer` - open the drawer
41+
* `closeDrawer` - close the drawer
42+
* `toggleDrawer` - toggle the state, ie. switch from closed to open and vice versa
2843

2944
## Common API reference
3045

31-
The vast majority of your interactions with the `navigation` prop will involve `navigate`, `goBack`, `state`, and `setParams`.
46+
The vast majority of your interactions with the `navigation` prop will involve `navigate`, `goBack`, `state`, and `setParams` / `getParams`.
3247

3348
### `navigate` - Link to other screens
3449

3550
Call this to link to another screen in your app. Takes the following arguments:
3651

37-
`navigation.navigate({routeName, params, action, key})`
52+
`navigation.navigate({ routeName, params, action, key })`
53+
3854
OR
55+
3956
`navigation.navigate(routeName, params, action)`
4057

4158
* `routeName` - A destination routeName that has been registered somewhere in the app's router
@@ -108,8 +125,8 @@ Alternatively, as _screen A_ is the top of the stack, you can use `navigation.po
108125
React Navigation emits events to screen components that subscribe to them:
109126

110127
* `willFocus` - the screen will focus
111-
* `willBlur` - the screen will be unfocused
112128
* `didFocus` - the screen focused (if there was a transition, the transition completed)
129+
* `willBlur` - the screen will be unfocused
113130
* `didBlur` - the screen unfocused (if there was a transition, the transition completed)
114131

115132
Example:
@@ -218,7 +235,7 @@ The following actions will work within any stack navigator:
218235

219236
### Push
220237

221-
Similar to navigate, push will move you forward to a new route in the stack.
238+
Similar to navigate, push will move you forward to a new route in the stack. This differs from `navigate` in that `navigate will` pop back to earlier in the stack if a route of the given name is already present there. `push` will always add on top, so a route can be present multiple times.
222239

223240
`navigation.push(routeName, params, action)`
224241

@@ -244,6 +261,19 @@ Call this to replace the current screen with the given route, with params and su
244261

245262
`navigation.replace(routeName, params, action)`
246263

264+
### Reset
265+
266+
Wipe the navigator state and replace it with the result of several actions.
267+
268+
`navigation.reset([NavigationActions.navigate({ routeName: 'Profile' })], 0)`
269+
270+
### Dismiss
271+
272+
Call this if you're in a nested (child) stack and want to dismiss the entire stack, returning to the parent stack.
273+
274+
`navigation.dismiss()`
275+
276+
247277
## Advanced API Reference
248278

249279
The `dispatch` function is much less commonly used, but a good escape hatch if you can't do what you need with `navigate` and `goBack`.
@@ -268,3 +298,25 @@ const navigateAction = NavigationActions.navigate({
268298
});
269299
this.props.navigation.dispatch(navigateAction);
270300
```
301+
302+
### `dangerouslyGetParent` - get parent navigator
303+
304+
If, for example, you have a screen component that can be presented within multiple navigators, you may use this to influence its behavior based on what navigator it is in. Be sure to always check that the call returns a valid value.
305+
306+
```js
307+
class UserCreateScreen extends Component {
308+
static navigationOptions = ({ navigation }) => {
309+
const parent = navigation.dangerouslyGetParent();
310+
const gesturesEnabled =
311+
parent &&
312+
parent.state &&
313+
parent.state.routeName === 'StackWithEnabledGestures';
314+
315+
return {
316+
title: 'New User',
317+
gesturesEnabled
318+
};
319+
};
320+
}
321+
322+
```

docs/stack-actions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The `replace` action replaces the route at the given key with another route.
6464

6565
### push
6666

67-
The `push` action adds a route on top of the stack and navigates forward to it. This differs from `navigate` in that `navigate` will pop back to earlier in the stack if a component is already mounted there. `push` will always add on top, so a component can be mounted multiple times.
67+
The `push` action adds a route on top of the stack and navigates forward to it. This differs from `navigate` in that `navigate` will pop back to earlier in the stack if a route of the given name is already present there. `push` will always add on top, so a route can be present multiple times.
6868

6969
* `routeName` - _string_ - `routeName` to push onto the stack.
7070
* `params` - _object_ - Screen params to merge into the destination route (found in the pushed screen through `this.props.navigation.state.params`).

website/i18n/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"customize-transitions": "customize-transitions",
3535
"deep-linking": "Deep linking",
3636
"Deep linking": "Deep linking",
37+
"drawer-actions": "DrawerActions reference",
38+
"DrawerActions": "DrawerActions",
3739
"drawer-based-navigation": "Drawer navigation",
3840
"Drawer navigation": "Drawer navigation",
3941
"drawer-navigator": "createDrawerNavigator",

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"navigation-prop",
5151
"navigation-actions",
5252
"stack-actions",
53+
"drawer-actions",
5354
"stack-navigator",
5455
"switch-navigator",
5556
"drawer-navigator",

0 commit comments

Comments
 (0)