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
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/typescript.md
+11-11
Original file line number
Diff line number
Diff line change
@@ -135,7 +135,7 @@ Now, when using `StaticParamList<typeof RootStack>`, it will include the screens
135
135
136
136
When using the dynamic API, it is necessary to specify the types for each screen as well as the nesting structure as it cannot be inferred from the code.
137
137
138
-
###Typechecking the navigator
138
+
## Typechecking the navigator
139
139
140
140
To typecheck our route name and params, the first thing we need to do is to create an object type with mappings for route names to the params of the route. For example, say we have a route called `Profile` in our root navigator which should have a param `userId`:
141
141
@@ -187,7 +187,7 @@ The type containing the mapping must be a type alias (e.g. `type RootStackParamL
187
187
188
188
:::
189
189
190
-
###Type checking screens
190
+
## Type checking screens
191
191
192
192
To typecheck our screens, we need to annotate the `navigation` and the `route` props received by a screen. The navigator packages in React Navigation export generic types to define types for both the `navigation` and `route` props from the corresponding navigator.
193
193
@@ -274,9 +274,9 @@ type ProfileScreenRouteProp = RouteProp<RootStackParamList, 'Profile'>;
274
274
275
275
We recommend creating a separate file: `types.tsx` - where you keep the types and import from there in your component files instead of repeating them in each file.
276
276
277
-
###Nesting navigators
277
+
## Nesting navigators
278
278
279
-
####Type checking screens and params in nested navigator
279
+
### Type checking screens and params in nested navigator
280
280
281
281
You can [navigate to a screen in a nested navigator](nesting-navigators.md#navigating-to-a-screen-in-a-nested-navigator) by passing `screen` and `params` properties for the nested screen:
282
282
@@ -298,7 +298,7 @@ type TabParamList = {
298
298
};
299
299
```
300
300
301
-
####Combining navigation props
301
+
### Combining navigation props
302
302
303
303
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:
304
304
@@ -340,7 +340,7 @@ type ProfileScreenNavigationProp = CompositeNavigationProp<
340
340
>;
341
341
```
342
342
343
-
###Annotating `useNavigation`
343
+
## Annotating `useNavigation`
344
344
345
345
:::danger
346
346
@@ -355,7 +355,7 @@ To annotate the `navigation` object that we get from `useNavigation`, we can use
@@ -370,7 +370,7 @@ To annotate the `route` object that we get from `useRoute`, we can use a type pa
370
370
const route =useRoute<ProfileScreenRouteProp>();
371
371
```
372
372
373
-
###Annotating `options` and `screenOptions`
373
+
## Annotating `options` and `screenOptions`
374
374
375
375
When you pass the `options` to a `Screen` or `screenOptions` prop to a `Navigator` component, they are already type-checked and you don't need to do anything special. However, sometimes you might want to extract the options to a separate object, and you might want to annotate it.
###Specifying default types for `useNavigation`, `Link`, `ref` etc
450
+
## Specifying default types for `useNavigation`, `Link`, `ref` etc
451
451
452
452
Instead of manually annotating these APIs, you can specify a global type for your root navigator which will be used as the default type.
453
453
@@ -465,7 +465,7 @@ The `RootParamList` interface lets React Navigation know about the params accept
465
465
466
466
Specifying this type is important if you heavily use `useNavigation`, `Link` etc. in your app since it'll ensure type-safety. It will also make sure that you have correct nesting on the `linking` prop.
467
467
468
-
###Organizing types
468
+
## Organizing types
469
469
470
470
When writing types for React Navigation, there are a couple of things we recommend to keep things organized.
0 commit comments