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
Now, we have an already working navigator, even though it doesn't do anything special yet.
@@ -202,6 +205,50 @@ function App() {
202
205
}
203
206
```
204
207
208
+
### `createScreenFactory`
209
+
210
+
This `createScreenFactory` function is used to create a typed screen factory function, which takes a screen configuration object for proper type-checking in static configuration.
211
+
212
+
Custom navigators should use `createScreenFactory` with appropriate types to create the screen factory function and export it.
Now, we can customize it to add additional functionality or change the behavior. For example, use a [custom router](custom-routers.md) instead of the default [`TabRouter`](custom-routers.md#built-in-routers):
The configuration object for a screen accepts the [properties described in the Screen page](screen.md). In addition, the following properties are available when using static configuration:
123
123
124
+
#### `createXScreen`
125
+
126
+
Each navigator exports a helper function to create screen configurations with proper TypeScript types. These helpers enable type inference for the params in the configuration callbacks such as `options` and `listeners`.
127
+
128
+
Example usage:
129
+
130
+
```js
131
+
import {
132
+
createNativeStackNavigator,
133
+
createNativeStackScreen,
134
+
} from'@react-navigation/native-stack';
135
+
136
+
constStack=createNativeStackNavigator({
137
+
screens: {
138
+
Profile:createNativeStackScreen({
139
+
screen: ProfileScreen,
140
+
options: ({ route }) => {
141
+
constuserId=route.params.userId;
142
+
143
+
return {
144
+
title:`${userId}'s profile`,
145
+
};
146
+
},
147
+
}),
148
+
},
149
+
});
150
+
```
151
+
152
+
Each navigator exports its own helper function:
153
+
154
+
-`createNativeStackScreen` from `@react-navigation/native-stack`
155
+
-`createStackScreen` from `@react-navigation/stack`
156
+
-`createBottomTabScreen` from `@react-navigation/bottom-tabs`
157
+
-`createDrawerScreen` from `@react-navigation/drawer`
158
+
-`createMaterialTopTabScreen` from `@react-navigation/material-top-tabs`
159
+
124
160
#### `linking`
125
161
126
162
[Linking configuration](configuring-links.md) for the screen. It can be either a string for a path or an object with the linking configuration:
The [`createXScreen`](static-configuration.md#createxscreen) helper is for type inference in `options` and `listeners` callbacks. Each navigator exports its own screen helper: [`createNativeStackScreen`](native-stack-navigator.md), [`createStackScreen`](stack-navigator.md), [`createBottomTabScreen`](bottom-tab-navigator.md), [`createDrawerScreen`](drawer-navigator.md), [`createMaterialTopTabScreen`](material-top-tab-navigator.md).
172
+
166
173
## Conditional screens
167
174
168
175
In the dynamic API, screens can be conditionally defined by rendering `Screen` components conditionally. In the static API, the [`if`](static-configuration.md#if) property can be used to conditionally render screens based on a hook returning a boolean.
@@ -590,6 +597,10 @@ function ProfileScreen({ route }: ProfileScreenProps) {
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/typescript.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,6 +63,36 @@ There are 2 steps to configure TypeScript with the static API:
63
63
64
64
This is needed to type-check the [`useNavigation`](use-navigation.md) hook.
65
65
66
+
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:
67
+
68
+
-`createNativeStackScreen` from `@react-navigation/native-stack`
69
+
-`createStackScreen` from `@react-navigation/stack`
70
+
-`createBottomTabScreen` from `@react-navigation/bottom-tabs`
71
+
-`createDrawerScreen` from `@react-navigation/drawer`
72
+
-`createMaterialTopTabScreen` from `@react-navigation/material-top-tabs`
73
+
74
+
For example:
75
+
76
+
```ts
77
+
import {
78
+
createNativeStackNavigator,
79
+
createNativeStackScreen,
80
+
} from'@react-navigation/native-stack';
81
+
82
+
const RootStack =createNativeStackNavigator({
83
+
screens: {
84
+
Profile: createNativeStackScreen({
85
+
screen: ProfileScreen,
86
+
options: ({ route }) => ({
87
+
title: route.params.username,
88
+
}),
89
+
}),
90
+
},
91
+
});
92
+
```
93
+
94
+
See [Static configuration](static-configuration.md#createxscreen) for more details.
95
+
66
96
## Navigator specific types
67
97
68
98
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, e.g. `setOptions` to update navigator options, `push`, `pop`, `popTo` etc. with stacks, `openDrawer`, `closeDrawer` etc. with drawer and so on, you need to manually annotate [`useNavigation`](use-navigation.md):
Copy file name to clipboardExpand all lines: versioned_docs/version-8.x/upgrading-from-7.x.md
+41-39Lines changed: 41 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -136,45 +136,6 @@ If you're using dynamic configuration, you can use `as`:
136
136
+ const focusedRouteName = useNavigationState((state) => state.routes[state.index].name as keyof RootStackParamList);
137
137
```
138
138
139
-
#### New `createXScreen` API for creating screen config
140
-
141
-
One of the limitations of the static config API is that the type of `route` object can't be inferred in screen callback, listeners callback etc. This made it difficult to use route params in these callbacks.
142
-
143
-
To address this, we added a new `createXScreen` API for each navigator to create screen config with proper types:
144
-
145
-
```diff lang=js
146
-
const Stack = createStackNavigator({
147
-
screens: {
148
-
- Profile: {
149
-
- screen: ProfileScreen,
150
-
- options: ({ route }) => {
151
-
- const userId = route.params.userId; // Don't know the type of route params
152
-
-
153
-
- return { title: `User ${userId}` };
154
-
- },
155
-
- },
156
-
+ Profile: createStackScreen({
157
-
+ screen: ProfileScreen,
158
-
+ options: ({ route }) => {
159
-
+ const userId = route.params.userId; // Now correctly inferred
160
-
+
161
-
+ return { title: `User ${userId}` };
162
-
+ },
163
-
+ });
164
-
}
165
-
});
166
-
```
167
-
168
-
When using the `createXScreen` API, the type of params are automatically inferred based on the type annotation for the component specified in `screen` (e.g. `(props: StaticScreenProps<ProfileParams>)`) and the path pattern specified in the linking config (e.g. `linking: 'profile/:userId'`).
169
-
170
-
Each navigator exports its own helper function, e.g. `createNativeStackScreen` for Native Stack Navigator, `createBottomTabScreen` for Bottom Tab Navigator, `createDrawerScreen` for Drawer Navigator etc.
171
-
172
-
:::note
173
-
174
-
This is technically not a breaking change. It's not required to use this API and your existing code will continue to work as before. You can incrementally adopt this API for new screens to get proper types for `route` object in various callbacks such as `options`, `listeners`, etc.
175
-
176
-
:::
177
-
178
139
See [Static configuration docs](static-configuration.md#createxscreen) for more details.
179
140
180
141
#### Custom navigators have a simpler type API
@@ -1040,6 +1001,47 @@ See the [`server rendering guide`](server-rendering.md) for a detailed guide and
1040
1001
1041
1002
## New features
1042
1003
1004
+
### `createXScreen` let's you create screen config with proper types
1005
+
1006
+
One of the limitations of the static config API is that the type of `route` object can't be inferred in screen callback, listeners callback etc. This made it difficult to use route params in these callbacks.
1007
+
1008
+
To address this, we added a new `createXScreen` API for each navigator to create screen config with proper types:
1009
+
1010
+
```diff lang=js
1011
+
const Stack = createStackNavigator({
1012
+
screens: {
1013
+
- Profile: {
1014
+
- screen: ProfileScreen,
1015
+
- options: ({ route }) => {
1016
+
- const userId = route.params.userId; // Don't know the type of route params
1017
+
-
1018
+
- return { title: `User ${userId}` };
1019
+
- },
1020
+
- },
1021
+
+ Profile: createStackScreen({
1022
+
+ screen: ProfileScreen,
1023
+
+ options: ({ route }) => {
1024
+
+ const userId = route.params.userId; // Now correctly inferred
1025
+
+
1026
+
+ return { title: `User ${userId}` };
1027
+
+ },
1028
+
+ });
1029
+
}
1030
+
});
1031
+
```
1032
+
1033
+
When using the `createXScreen` API, the type of params are automatically inferred based on the type annotation for the component specified in `screen` (e.g. `(props: StaticScreenProps<ProfileParams>)`) and the path pattern specified in the linking config (e.g. `linking: 'profile/:userId'`).
1034
+
1035
+
Each navigator exports its own helper function, e.g. `createNativeStackScreen` for Native Stack Navigator, `createBottomTabScreen` for Bottom Tab Navigator, `createDrawerScreen` for Drawer Navigator etc.
1036
+
1037
+
This API has also been backported to React Navigation 7.
1038
+
1039
+
:::note
1040
+
1041
+
It's not required to use this API and your existing code will continue to work as before. You can incrementally adopt this API to get proper types for `route` object in various callbacks such as `options`, `listeners`, etc.
1042
+
1043
+
:::
1044
+
1043
1045
### Common hooks now accept name of the screen
1044
1046
1045
1047
The `useNavigation`, `useRoute`, and `useNavigationState` hooks can now optionally accept the name of the screen:
0 commit comments