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-6.x/typescript.md
+60-83Lines changed: 60 additions & 83 deletions
Original file line number
Diff line number
Diff line change
@@ -54,83 +54,27 @@ This will provide type checking and intelliSense for props of the `Navigator` an
54
54
55
55
### Type checking screens
56
56
57
-
To type check our screens, we need to annotate the `navigation` prop and the `route` prop received by a screen.
57
+
To type check our screens, we need to annotate the `navigation` prop and the `route` prop received by a screen. The navigator packages in React Navigation export a generic types to define types for both the `navigation` and `route` props from the corresponding navigator.
58
58
59
-
To annotate the `navigation` prop, we need to import the corresponding type from the navigator. For example, `StackNavigationProp` for `@react-navigation/stack`:
59
+
For example, you can use `NativeStackScreenProps` for the Native Stack Navigator.
The type for the navigation prop takes 2 generics, the param list object we defined earlier, and the name of the current route. This allows us to type check route names and params which you're navigating using `navigate`, `push` etc. The name of the current route is necessary to type check the params when you call `setParams`.
75
-
76
-
Similarly, you can import `DrawerNavigationProp` from `@react-navigation/drawer`, `BottomTabNavigationProp` from `@react-navigation/bottom-tabs` etc.
77
-
78
-
To annotate the `route` prop, we need to use the `RouteProp` type from `@react-navigation/native`:
Alternatively, you can also import a generic type to define types for both the `navigation` and `route` props from the corresponding navigator:
73
+
The type takes 2 generics, the param list object we defined earlier, and the name of the current route. This allows us to type check route names and params which you're navigating using `navigate`, `push` etc. The name of the current route is necessary to type check the params in `route.params` and when you call `setParams`.
Similarly, you can import `StackScreenProps` for `@react-navigation/stack`, `DrawerScreenProps` from `@react-navigation/drawer`, `BottomTabScreenProps` from `@react-navigation/bottom-tabs` and so on.
Alternatively, you can also annotate the `navigation` and `route` props separately.
106
+
107
+
To get the type for the `navigation` prop, we need to import the corresponding type from the navigator. For example, `NativeStackNavigationProp` for `@react-navigation/native-stack`:
Similarly, you can import `StackNavigationProp` from `@react-navigation/stack`, `DrawerNavigationProp` from `@react-navigation/drawer`, `BottomTabNavigationProp` from `@react-navigation/bottom-tabs` etc.
119
+
120
+
To get the type for the `route` prop, we need to use the `RouteProp` type from `@react-navigation/native`:
We recommend creating a separate `types.tsx` file where you keep the types and import them in your component files instead of repeating them in each file.
154
129
155
130
### Nesting navigators
@@ -178,43 +153,43 @@ type TabParamList = {
178
153
179
154
#### Combining navigation props
180
155
181
-
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 navigator, you can use the `CompositeNavigationProp` type:
156
+
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:
The `CompositeNavigationProp` type takes 2 parameters, first parameter is the primary navigation type (type for the navigator that owns this screen, in our case the tab navigator which contains the `Profile` screen) and second parameter is the secondary navigation type (type for a parent navigator). The primary navigation type should always have the screen's route name as it's second parameter.
169
+
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 it's second parameter.
195
170
196
171
For multiple parent navigators, this secondary type should be nested:
Similarly, you can import `DrawerNavigationOptions` from `@react-navigation/drawer`, `BottomTabNavigationOptions` from `@react-navigation/bottom-tabs` etc.
256
231
232
+
When using the function form of `options` and `screenOptions`, you can annotate the arguments with the same type you used to annotate the `navigation` and `route` props.
233
+
257
234
### Annotating `ref` on `NavigationContainer`
258
235
259
236
If you use the `createNavigationContainerRef()` method to create the ref, you can annotate it to type-check navigation actions:
0 commit comments