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-8.x/typescript.md
+20-14Lines changed: 20 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,24 @@ After setting up the type for the root navigator, all we need to do is specify t
57
57
58
58
This can be done in 2 ways:
59
59
60
-
1. The type annotation for the component specified in `screen`:
60
+
1. The path pattern specified in the linking config (e.g. for `path: 'profile/:userId'`, the type of `route.params` is `{ userId: string }`). The type can be further customized by using a [`parse` function in the linking config](configuring-links.md#passing-params):
61
+
62
+
```ts
63
+
linking: {
64
+
// highlight-start
65
+
path: 'profile/:userId',
66
+
parse: {
67
+
userId: (id) =>parseInt(id, 10),
68
+
},
69
+
// highlight-end
70
+
},
71
+
```
72
+
73
+
The above example would make the type of `route.params` be `{ userId: number }` since the `parse` function converts the string from the URL to a number.
74
+
75
+
This is the recommended way to specify params for screens that are accessible via deep linking or if your app runs on the Web, as it ensures that the types of params are consistent with the URL.
76
+
77
+
2. The type annotation for the component specified in `screen`:
2. The path pattern specified in the linking config (e.g. for `linking: 'profile/:userId'`, the type of `route.params` is `{ userId: string }`). The type can be further customized by using a [`parse` function in the linking config](configuring-links.md#passing-params):
89
-
90
-
```ts
91
-
linking: {
92
-
path: 'profile/:userId',
93
-
parse: {
94
-
userId: (id) =>parseInt(id, 10),
95
-
},
96
-
},
97
-
```
98
-
99
105
The above example would make the type of `route.params` be `{ userId: number }` since the `parse` function converts the string from the URL to a number.
100
106
107
+
If your app supports deep linking or runs on the Web, you can use this pattern to specify any additional optional params that don't appear in the path pattern (e.g. query params). Make sure to add `| undefined` to the type of params to make them optional, as query params may not be present in the URL.
108
+
101
109
If both `screen` and `linking` specify params, the final type of `route.params` is the intersection of both types.
If your app supports deep linking or runs on the Web, it is recommended to specify params that appear in the path pattern in the linking config. Any additional params (e.g. query params) can be specified in the component's props.
124
-
125
131
If you have specified the params in `linking`, it's recommended to not specify them again in the component's props, and use `useRoute('ScreenName')` instead to get the correctly typed `route` object.
126
132
127
133
The `createXScreen` helper functions enable type inference in screen configuration callbacks like `options`, `listeners`, etc. Each navigator exports its own version of the helper function:
0 commit comments