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
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`.
91
+
This allows us to type check route names and params which you're navigating using `navigate`, [`push`](stack-actions.md#push) etc. The name of the current route is necessary to type check the params in `route.params` and when you call [`setParams`](navigation-actions#setparams).
92
92
93
-
Similarly, you can import `StackScreenProps`for `@react-navigation/stack`, `DrawerScreenProps` from `@react-navigation/drawer`, `BottomTabScreenProps` from `@react-navigation/bottom-tabs` and so on.
93
+
Similarly, you can import `StackScreenProps`from [`@react-navigation/stack`](stack-navigator.md), `DrawerScreenProps` from [`@react-navigation/drawer`](drawer-navigator.md), `BottomTabScreenProps` from [`@react-navigation/bottom-tabs`](bottom-tab-navigator.md) and so on.
94
94
95
95
Then you can use the `Props` type you defined above to annotate your component.
96
96
@@ -133,7 +133,7 @@ type ProfileScreenNavigationProp = NativeStackNavigationProp<
133
133
>;
134
134
```
135
135
136
-
Similarly, you can import `StackNavigationProp` from `@react-navigation/stack`, `DrawerNavigationProp` from `@react-navigation/drawer`, `BottomTabNavigationProp` from `@react-navigation/bottom-tabs` etc.
136
+
Similarly, you can import `StackNavigationProp` from [`@react-navigation/stack`](stack-navigator.md), `DrawerNavigationProp` from [`@react-navigation/drawer`](drawer-navigator.md), `BottomTabNavigationProp` from [`@react-navigation/bottom-tabs`](bottom-tab-navigator.md) etc.
137
137
138
138
To get the type for the `route` prop, we need to use the `RouteProp` type from `@react-navigation/native`:
139
139
@@ -321,7 +321,7 @@ declare global {
321
321
322
322
The `RootParamList` interface lets React Navigation know about the params accepted by your root navigator. Here we extend the type `RootStackParamList` because that's the type of params for our stack navigator at the root. The name of this type isn't important.
323
323
324
-
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.
324
+
Specifying this type is important if you heavily use `useNavigation`, [`Link`](link.md) etc. in your app since it'll ensure type-safety. It will also make sure that you have correct nesting on the [`linking`](navigation-container.md#linking) prop.
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/navigation-container.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -166,6 +166,8 @@ export default function App() {
166
166
167
167
If you're using a regular ref object, keep in mind that the ref may be initially `null` in some situations (such as when linking is enabled). To make sure that the ref is initialized, you can use the [`onReady`](#onready) callback to get notified when the navigation container finishes mounting.
168
168
169
+
Check how to setup `ref` with TypeScript [here](typescript.md#annotating-ref-on-navigationcontainer).
170
+
169
171
See the [Navigating without the navigation prop](navigating-without-navigation-prop.md) guide for more details.
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/screen-options.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,8 @@ Each screen can configure various aspects about how it gets presented in the nav
11
11
12
12
In the [configuring the header bar](headers.md) section of the fundamentals documentation we explain the basics of how this works. Also see the [screen options resolution guide](screen-options-resolution.md) to get an idea of how they work when there are multiple navigators.
13
13
14
+
See [our docs](typescript.md#annotating-options-and-screenoptions) to learn more about how to use TypeScript with `screenOptions` and `options`.
15
+
14
16
There are 3 ways of specifying options for screens:
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/typescript.md
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ React Navigation can be configured to type-check screens and their params, as we
14
14
15
15
There are 2 steps to configure TypeScript with the static API:
16
16
17
-
1. Each screen component needs to specify the type of the `route.params` prop that it accepts. The `StaticScreenProps` type makes it simpler:
17
+
1. Each screen component needs to specify the type of the [`route.params`](params.md) prop that it accepts. The `StaticScreenProps` type makes it simpler:
@@ -60,11 +60,11 @@ There are 2 steps to configure TypeScript with the static API:
60
60
// highlight-end
61
61
```
62
62
63
-
This is needed to type-check the `useNavigation` hook.
63
+
This is needed to type-check the [`useNavigation`](use-navigation.md) hook.
64
64
65
65
## Navigator specific types
66
66
67
-
Generally, we recommend using the default types for the `useNavigation` prop to access the navigation object in a navigator-agnostic manner. However, if you need to use navigator-specific APIs, you need to manually annotate `useNavigation`:
67
+
Generally, we recommend using the default types for the [`useNavigation`](use-navigation.md) prop to access the navigation object in a navigator-agnostic manner. However, if you need to use navigator-specific APIs, you need to manually annotate [`useNavigation`](use-navigation.md):
Note that annotating `useNavigation` this way is not type-safe since we can't guarantee that the type you provided matches the type of the navigator.
81
+
Note that annotating [`useNavigation`](use-navigation.md) this way is not type-safe since we can't guarantee that the type you provided matches the type of the navigator.
Here, the `HomeTabs` component is defined using the dynamic API. This means that when we create the param list for the root navigator with `StaticParamList<typeof RootStack>`, it won't know about the screens defined in the nested navigator. To fix this, we'd need to specify the param list for the nested navigator explicitly.
105
105
106
-
This can be done by using the type of the `route` prop that the screen component receives:
106
+
This can be done by using the type of the [`route`](route-object.md) prop that the screen component receives:
107
107
108
108
```ts
109
109
typeHomeTabsParamList= {
@@ -157,7 +157,7 @@ type RootStackParamList = {
157
157
158
158
Specifying `undefined` means that the route doesn't have params. A union type with `undefined` (e.g. `SomeType | undefined`) means that params are optional.
159
159
160
-
After we have defined the mapping, we need to tell our navigator to use it. To do that, we can pass it as a generic to the `createXNavigator` functions:
160
+
After we have defined the mapping, we need to tell our navigator to use it. To do that, we can pass it as a generic to the [`createXNavigator`](static-configuration.md) functions:
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`.
220
+
This allows us to type check route names and params which you're navigating using [`navigate`](navigation-object.md#navigate), [`push`](stack-actions.md#push) etc. The name of the current route is necessary to type check the params in `route.params` and when you call [`setParams`](navigation-actions#setparams).
221
221
222
-
Similarly, you can import `StackScreenProps`for `@react-navigation/stack`, `DrawerScreenProps` from `@react-navigation/drawer`, `BottomTabScreenProps` from `@react-navigation/bottom-tabs` and so on.
222
+
Similarly, you can import `StackScreenProps`from [`@react-navigation/stack`](stack-navigator.md), `DrawerScreenProps` from [`@react-navigation/drawer`](drawer-navigator.md), `BottomTabScreenProps` from [`@react-navigation/bottom-tabs`](bottom-tab-navigator.md) and so on.
223
223
224
224
Then you can use the `Props` type you defined above to annotate your component.
225
225
@@ -262,7 +262,7 @@ type ProfileScreenNavigationProp = NativeStackNavigationProp<
262
262
>;
263
263
```
264
264
265
-
Similarly, you can import `StackNavigationProp` from `@react-navigation/stack`, `DrawerNavigationProp` from `@react-navigation/drawer`, `BottomTabNavigationProp` from `@react-navigation/bottom-tabs` etc.
265
+
Similarly, you can import `StackNavigationProp` from [`@react-navigation/stack`](stack-navigator.md), `DrawerNavigationProp` from [`@react-navigation/drawer`](drawer-navigator.md), `BottomTabNavigationProp` from [`@react-navigation/bottom-tabs`](bottom-tab-navigator.md) etc.
266
266
267
267
To get the type for the `route` object, we need to use the `RouteProp` type from `@react-navigation/native`:
268
268
@@ -300,7 +300,7 @@ type TabParamList = {
300
300
301
301
### Combining navigation props
302
302
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:
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`](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:
@@ -364,7 +364,7 @@ Prefer using the [`route` object](route-object.md) from the screen component's p
364
364
365
365
:::
366
366
367
-
To annotate the `route` object that we get from `useRoute`, we can use a type parameter:
367
+
To annotate the `route` object that we get from [`useRoute`](use-route.md), we can use a type parameter:
368
368
369
369
```ts
370
370
const route =useRoute<ProfileScreenRouteProp>();
@@ -463,7 +463,7 @@ declare global {
463
463
464
464
The `RootParamList` interface lets React Navigation know about the params accepted by your root navigator. Here we extend the type `RootStackParamList` because that's the type of params for our stack navigator at the root. The name of this type isn't important.
465
465
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.
466
+
Specifying this type is important if you heavily use [`useNavigation`](use-navigation.md), [`Link`](link.md) etc. in your app since it'll ensure type-safety. It will also make sure that you have correct nesting on the [`linking`](navigation-container.md#linking) prop.
0 commit comments