Skip to content

Commit a80a928

Browse files
committed
Improve docs for navigation actions
1 parent 5a8fdc8 commit a80a928

18 files changed

Lines changed: 552 additions & 504 deletions

versioned_docs/version-7.x/bottom-tab-navigator.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,8 @@ export default function App() {
752752
}
753753
```
754754
755+
See [`jumpTo`](tab-actions.md#jumpto) for more details.
756+
755757
### Hooks
756758
757759
The bottom tab navigator exports the following hooks:

versioned_docs/version-7.x/drawer-actions.md

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import TabItem from '@theme/TabItem';
99

1010
`DrawerActions` is an object containing methods for generating actions specific to drawer-based navigators. Its methods expand upon the actions available in [CommonActions](navigation-actions.md).
1111

12+
For screens inside a [Drawer Navigator](drawer-navigator.md), drawer actions are available as methods on the `navigation` object.
13+
1214
The following actions are supported:
1315

1416
## openDrawer
@@ -34,7 +36,7 @@ function HomeScreen() {
3436
<Button
3537
onPress={() => {
3638
// codeblock-focus-start
37-
navigation.dispatch(DrawerActions.openDrawer());
39+
navigation.openDrawer();
3840
// codeblock-focus-end
3941
}}
4042
>
@@ -57,6 +59,14 @@ export default function App() {
5759
}
5860
```
5961

62+
It can also be used with `navigation.dispatch`:
63+
64+
```js
65+
import { DrawerActions } from '@react-navigation/native';
66+
67+
navigation.dispatch(DrawerActions.openDrawer());
68+
```
69+
6070
## closeDrawer
6171

6272
The `closeDrawer` action can be used to close the drawer pane.
@@ -68,7 +78,6 @@ import { Button } from '@react-navigation/elements';
6878
import {
6979
createStaticNavigation,
7080
useNavigation,
71-
DrawerActions,
7281
} from '@react-navigation/native';
7382
import {
7483
createDrawerNavigator,
@@ -83,9 +92,7 @@ function HomeScreen() {
8392
return (
8493
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
8594
<Text>Home!</Text>
86-
<Button onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
87-
Open Drawer
88-
</Button>
95+
<Button onPress={() => navigation.openDrawer()}>Open Drawer</Button>
8996
</View>
9097
);
9198
}
@@ -100,7 +107,7 @@ function CustomDrawerContent(props) {
100107
label="Close drawer"
101108
onPress={() => {
102109
// codeblock-focus-start
103-
navigation.dispatch(DrawerActions.closeDrawer());
110+
navigation.closeDrawer();
104111
// codeblock-focus-end
105112
}}
106113
/>
@@ -122,6 +129,14 @@ export default function App() {
122129
}
123130
```
124131

132+
It can also be used with `navigation.dispatch`:
133+
134+
```js
135+
import { DrawerActions } from '@react-navigation/native';
136+
137+
navigation.dispatch(DrawerActions.closeDrawer());
138+
```
139+
125140
## toggleDrawer
126141

127142
The `toggleDrawer` action can be used to toggle the drawer pane.
@@ -133,48 +148,29 @@ import { Button } from '@react-navigation/elements';
133148
import {
134149
createStaticNavigation,
135150
useNavigation,
136-
DrawerActions,
137151
} from '@react-navigation/native';
138-
import {
139-
createDrawerNavigator,
140-
DrawerContentScrollView,
141-
DrawerItemList,
142-
DrawerItem,
143-
} from '@react-navigation/drawer';
152+
import { createDrawerNavigator } from '@react-navigation/drawer';
144153

145154
function HomeScreen() {
146155
const navigation = useNavigation();
147156

148157
return (
149158
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
150159
<Text>Home!</Text>
151-
<Button onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}>
152-
Toggle Drawer
153-
</Button>
154-
</View>
155-
);
156-
}
157-
158-
function CustomDrawerContent(props) {
159-
const { navigation } = props;
160-
161-
return (
162-
<DrawerContentScrollView {...props}>
163-
<DrawerItemList {...props} />
164-
<DrawerItem
165-
label="Toggle drawer"
160+
<Button
166161
onPress={() => {
167162
// codeblock-focus-start
168-
navigation.dispatch(DrawerActions.toggleDrawer());
163+
navigation.toggleDrawer();
169164
// codeblock-focus-end
170165
}}
171-
/>
172-
</DrawerContentScrollView>
166+
>
167+
Toggle Drawer
168+
</Button>
169+
</View>
173170
);
174171
}
175172

176173
const MyDrawer = createDrawerNavigator({
177-
drawerContent: (props) => <CustomDrawerContent {...props} />,
178174
screens: {
179175
Home: HomeScreen,
180176
},
@@ -187,6 +183,14 @@ export default function App() {
187183
}
188184
```
189185

186+
It can also be used with `navigation.dispatch`:
187+
188+
```js
189+
import { DrawerActions } from '@react-navigation/native';
190+
191+
navigation.dispatch(DrawerActions.toggleDrawer());
192+
```
193+
190194
## jumpTo
191195

192196
The `jumpTo` action can be used to jump to an existing route in the drawer navigator.
@@ -201,7 +205,6 @@ import { Button } from '@react-navigation/elements';
201205
import {
202206
createStaticNavigation,
203207
useNavigation,
204-
DrawerActions,
205208
} from '@react-navigation/native';
206209
import { createDrawerNavigator } from '@react-navigation/drawer';
207210

@@ -214,9 +217,7 @@ function HomeScreen() {
214217
<Button
215218
onPress={() => {
216219
// codeblock-focus-start
217-
navigation.dispatch(
218-
DrawerActions.jumpTo('Profile', { user: 'Satya' })
219-
);
220+
navigation.jumpTo('Profile', { user: 'Satya' });
220221
// codeblock-focus-end
221222
}}
222223
>
@@ -250,3 +251,11 @@ export default function App() {
250251
return <Navigation />;
251252
}
252253
```
254+
255+
It can also be used with `navigation.dispatch`:
256+
257+
```js
258+
import { DrawerActions } from '@react-navigation/native';
259+
260+
navigation.dispatch(DrawerActions.jumpTo('Profile', { user: 'Satya' }));
261+
```

versioned_docs/version-7.x/drawer-navigator.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,8 @@ export default function App() {
749749
}
750750
```
751751

752+
See [`openDrawer`](drawer-actions.md#opendrawer) for more details.
753+
752754
#### `closeDrawer`
753755

754756
Closes the drawer pane.
@@ -757,6 +759,8 @@ Closes the drawer pane.
757759
navigation.closeDrawer();
758760
```
759761

762+
See [`closeDrawer`](drawer-actions.md#closedrawer) for more details.
763+
760764
#### `toggleDrawer`
761765

762766
Opens the drawer pane if closed, closes the drawer pane if opened.
@@ -765,6 +769,8 @@ Opens the drawer pane if closed, closes the drawer pane if opened.
765769
navigation.toggleDrawer();
766770
```
767771

772+
See [`toggleDrawer`](drawer-actions.md#toggledrawer) for more details.
773+
768774
#### `jumpTo`
769775

770776
Navigates to an existing screen in the drawer navigator. The method accepts the following arguments:
@@ -827,6 +833,8 @@ export default function App() {
827833
}
828834
```
829835
836+
See [`jumpTo`](drawer-actions.md#jumpto) for more details.
837+
830838
### Hooks
831839
832840
The drawer navigator exports the following hooks:

versioned_docs/version-7.x/material-top-tab-navigator.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ export default function App() {
659659
}
660660
```
661661
662+
See [`jumpTo`](tab-actions.md#jumpto) for more details.
663+
662664
### Hooks
663665
664666
The material top tab navigator exports the following hooks:

versioned_docs/version-7.x/native-stack-navigator.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,8 @@ Replaces the current screen with a new screen in the stack. The method accepts t
13131313
navigation.replace('Profile', { owner: 'Michaś' });
13141314
```
13151315
1316+
See [`replace`](stack-actions.md#replace) for more details.
1317+
13161318
#### `push`
13171319
13181320
Pushes a new screen to the top of the stack and navigate to it. The method accepts the following arguments:
@@ -1324,6 +1326,8 @@ Pushes a new screen to the top of the stack and navigate to it. The method accep
13241326
navigation.push('Profile', { owner: 'Michaś' });
13251327
```
13261328
1329+
See [`push`](stack-actions.md#push) for more details.
1330+
13271331
#### `pop`
13281332
13291333
Pops the current screen from the stack and navigates back to the previous screen. It takes one optional argument (`count`), which allows you to specify how many screens to pop back by.
@@ -1332,6 +1336,8 @@ Pops the current screen from the stack and navigates back to the previous screen
13321336
navigation.pop();
13331337
```
13341338
1339+
See [`pop`](stack-actions.md#pop) for more details.
1340+
13351341
#### `popTo`
13361342
13371343
Navigates back to a previous screen in the stack by popping screens after it. The method accepts the following arguments:
@@ -1347,6 +1353,8 @@ If a matching screen is not found in the stack, this will pop the current screen
13471353
navigation.popTo('Profile', { owner: 'Michaś' });
13481354
```
13491355
1356+
See [`popTo`](stack-actions.md#popto) for more details.
1357+
13501358
#### `popToTop`
13511359
13521360
Pops all of the screens in the stack except the first one and navigates to it.
@@ -1355,6 +1363,8 @@ Pops all of the screens in the stack except the first one and navigates to it.
13551363
navigation.popToTop();
13561364
```
13571365
1366+
See [`popToTop`](stack-actions.md#poptotop) for more details.
1367+
13581368
### Hooks
13591369
13601370
The native stack navigator exports the following hooks:

0 commit comments

Comments
 (0)