Skip to content

Commit 0078b01

Browse files
committed
Update TypeScript docs for composite navigation props
1 parent 3f2ccb4 commit 0078b01

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

versioned_docs/version-6.x/typescript.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ type TabParamList = {
171171

172172
#### Combining navigation props
173173

174-
When you nest navigators, the navigation prop of the screen is a combination of multiple navigation props. For example, if we have a tab inside a stack, the `navigation` prop will have both `jumpTo` (from the tab navigator) and `push` (from the stack navigator). To make it easier to combine types from multiple navigators, you can use the `CompositeScreenProps` type:
174+
When you nest navigators, the navigation prop of the screen is a combination of multiple navigation props. For example, if we have a tab inside a stack, the `navigation` prop will have both [`jumpTo`](tab-actions.md#jumpto) (from the tab navigator) and [`push`](stack-actions.md#push) (from the stack navigator). To make it easier to combine types from multiple navigators, you can use the `CompositeScreenProps` type.
175+
176+
For example, if we have a `Profile` in a navigator, nested inside `Account` screen of a stack navigator, we can combine the types as follows:
175177

176178
```ts
177179
import type { CompositeScreenProps } from '@react-navigation/native';
@@ -180,20 +182,23 @@ import type { StackScreenProps } from '@react-navigation/stack';
180182

181183
type ProfileScreenProps = CompositeScreenProps<
182184
BottomTabScreenProps<TabParamList, 'Profile'>,
183-
StackScreenProps<StackParamList>
185+
StackScreenProps<StackParamList, 'Account'>
184186
>;
185187
```
186188

187-
The `CompositeScreenProps` type takes 2 parameters, first parameter is the type of props for the primary navigation (type for the navigator that owns this screen, in our case the tab navigator which contains the `Profile` screen) and second parameter is the type of props for secondary navigation (type for a parent navigator). The primary type should always have the screen's route name as its second parameter.
189+
The `CompositeScreenProps` type takes 2 parameters:
190+
191+
- The first parameter is the type for the navigator that owns this screen, in our case the tab navigator which contains the `Profile` screen
192+
- The second parameter is the type of props for a parent navigator, in our case the stack navigator which contains the `Account` screen
188193

189-
For multiple parent navigators, this secondary type should be nested:
194+
For multiple parent navigators, this second parameter can nest another `CompositeScreenProps`:
190195

191196
```ts
192197
type ProfileScreenProps = CompositeScreenProps<
193198
BottomTabScreenProps<TabParamList, 'Profile'>,
194199
CompositeScreenProps<
195-
StackScreenProps<StackParamList>,
196-
DrawerScreenProps<DrawerParamList>
200+
StackScreenProps<StackParamList, 'Account'>,
201+
DrawerScreenProps<DrawerParamList, 'Home'>
197202
>
198203
>;
199204
```
@@ -207,7 +212,7 @@ import type { StackNavigationProp } from '@react-navigation/stack';
207212

208213
type ProfileScreenNavigationProp = CompositeNavigationProp<
209214
BottomTabNavigationProp<TabParamList, 'Profile'>,
210-
StackNavigationProp<StackParamList>
215+
StackNavigationProp<StackParamList, 'Account'>
211216
>;
212217
```
213218

@@ -358,7 +363,7 @@ export type HomeTabParamList = {
358363
export type HomeTabScreenProps<T extends keyof HomeTabParamList> =
359364
CompositeScreenProps<
360365
BottomTabScreenProps<HomeTabParamList, T>,
361-
RootStackScreenProps<keyof RootStackParamList>
366+
RootStackScreenProps<keyof RootStackParamList, 'Home'>
362367
>;
363368

364369
declare global {

versioned_docs/version-7.x/typescript.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ type TabParamList = {
319319

320320
### Combining navigation props
321321

322-
When you nest navigators, the navigation prop of the screen is a combination of multiple navigation props. For example, if we have a tab inside a stack, the `navigation` prop will have both [`jumpTo`](tab-actions.md#jumpto) (from the tab navigator) and [`push`](stack-actions.md#push) (from the stack navigator). To make it easier to combine types from multiple navigators, you can use the `CompositeScreenProps` type:
322+
When you nest navigators, the navigation prop of the screen is a combination of multiple navigation props. For example, if we have a tab inside a stack, the `navigation` prop will have both [`jumpTo`](tab-actions.md#jumpto) (from the tab navigator) and [`push`](stack-actions.md#push) (from the stack navigator). To make it easier to combine types from multiple navigators, you can use the `CompositeScreenProps` type.
323+
324+
For example, if we have a `Profile` in a navigator, nested inside `Account` screen of a stack navigator, we can combine the types as follows:
323325

324326
```ts
325327
import type { CompositeScreenProps } from '@react-navigation/native';
@@ -328,20 +330,23 @@ import type { StackScreenProps } from '@react-navigation/stack';
328330

329331
type ProfileScreenProps = CompositeScreenProps<
330332
BottomTabScreenProps<TabParamList, 'Profile'>,
331-
StackScreenProps<StackParamList>
333+
StackScreenProps<StackParamList, 'Account'>
332334
>;
333335
```
334336

335-
The `CompositeScreenProps` type takes 2 parameters, the first parameter is the type of props for the primary navigation (type for the navigator that owns this screen, in our case the tab navigator which contains the `Profile` screen) and the second parameter is the type of props for secondary navigation (type for a parent navigator). The primary type should always have the screen's route name as its second parameter.
337+
The `CompositeScreenProps` type takes 2 parameters:
338+
339+
- The first parameter is the type for the navigator that owns this screen, in our case the tab navigator which contains the `Profile` screen
340+
- The second parameter is the type of props for a parent navigator, in our case the stack navigator which contains the `Account` screen
336341

337-
For multiple parent navigators, this secondary type should be nested:
342+
For multiple parent navigators, this second parameter can nest another `CompositeScreenProps`:
338343

339344
```ts
340345
type ProfileScreenProps = CompositeScreenProps<
341346
BottomTabScreenProps<TabParamList, 'Profile'>,
342347
CompositeScreenProps<
343-
StackScreenProps<StackParamList>,
344-
DrawerScreenProps<DrawerParamList>
348+
StackScreenProps<StackParamList, 'Account'>,
349+
DrawerScreenProps<DrawerParamList, 'Home'>
345350
>
346351
>;
347352
```
@@ -355,7 +360,7 @@ import type { StackNavigationProp } from '@react-navigation/stack';
355360

356361
type ProfileScreenNavigationProp = CompositeNavigationProp<
357362
BottomTabNavigationProp<TabParamList, 'Profile'>,
358-
StackNavigationProp<StackParamList>
363+
StackNavigationProp<StackParamList, 'Account'>
359364
>;
360365
```
361366

@@ -519,7 +524,7 @@ export type HomeTabParamList = {
519524
export type HomeTabScreenProps<T extends keyof HomeTabParamList> =
520525
CompositeScreenProps<
521526
BottomTabScreenProps<HomeTabParamList, T>,
522-
RootStackScreenProps<keyof RootStackParamList>
527+
RootStackScreenProps<keyof RootStackParamList, 'Home'>
523528
>;
524529

525530
declare global {

0 commit comments

Comments
 (0)