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/screen.md
+34-17Lines changed: 34 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,23 @@ A screen represents routes in a navigator. A screen's configuration contains the
14
14
15
15
Screens can be defined under the `screens` key in the navigator configuration:
16
16
17
+
```js
18
+
constStack=createNativeStackNavigator({
19
+
screens: {
20
+
Home:createNativeStackScreen({
21
+
screen: HomeScreen,
22
+
}),
23
+
Profile:createNativeStackScreen({
24
+
screen: ProfileScreen,
25
+
}),
26
+
},
27
+
});
28
+
```
29
+
30
+
The [`createXScreen`](static-configuration.md#createxscreen) helper is optional, but necessary for type inference in screen configuration callbacks such as `options`, `listeners`, etc.
31
+
32
+
A shorthand syntax is also supported where the screen component is used directly instead of a configuration object:
33
+
17
34
```js
18
35
constMyStack=createNativeStackNavigator({
19
36
screens: {
@@ -61,9 +78,9 @@ The key in the `screens` object is used as the name:
61
78
constStack=createNativeStackNavigator({
62
79
screens: {
63
80
// highlight-next-line
64
-
Profile: {
81
+
Profile:createNativeStackScreen({
65
82
screen: ProfileScreen,
66
-
},
83
+
}),
67
84
},
68
85
});
69
86
```
@@ -104,14 +121,14 @@ Options are used to configure how the screen gets presented in the navigator. It
104
121
```js
105
122
constStack=createNativeStackNavigator({
106
123
screens: {
107
-
Profile: {
124
+
Profile:createNativeStackScreen({
108
125
screen: ProfileScreen,
109
126
// highlight-start
110
127
options: {
111
128
title:'Awesome app',
112
129
},
113
130
// highlight-end
114
-
},
131
+
}),
115
132
},
116
133
});
117
134
```
@@ -142,14 +159,14 @@ When you pass a function, it'll receive the [`route`](route-object.md), [`naviga
142
159
```js
143
160
constStack=createNativeStackNavigator({
144
161
screens: {
145
-
Profile: {
162
+
Profile:createNativeStackScreen({
146
163
screen: ProfileScreen,
147
164
// highlight-start
148
165
options: ({ route, navigation, theme }) => ({
149
166
title:route.params.userId,
150
167
}),
151
168
// highlight-end
152
-
},
169
+
}),
153
170
},
154
171
});
155
172
```
@@ -184,11 +201,11 @@ Initial params are used as the default params for the screen. If a screen is use
184
201
```js
185
202
constStack=createNativeStackNavigator({
186
203
screens: {
187
-
Details: {
204
+
Details:createNativeStackScreen({
188
205
screen: DetailsScreen,
189
206
// highlight-next-line
190
207
initialParams: { itemId:42 },
191
-
},
208
+
}),
192
209
},
193
210
});
194
211
```
@@ -220,11 +237,11 @@ This can be done by specifying the `getId` callback. It receives an object with
220
237
```js
221
238
constStack=createStackNavigator({
222
239
screens: {
223
-
Profile: {
240
+
Profile:createStackScreen({
224
241
screen: ProfileScreen,
225
242
// highlight-next-line
226
243
getId: ({ params }) =>params.userId,
227
-
},
244
+
}),
228
245
},
229
246
});
230
247
```
@@ -273,10 +290,10 @@ It can be passed under the `screen` property in the screen configuration:
273
290
```js
274
291
constStack=createNativeStackNavigator({
275
292
screens: {
276
-
Profile: {
293
+
Profile:createNativeStackScreen({
277
294
// highlight-next-line
278
295
screen: ProfileScreen,
279
-
},
296
+
}),
280
297
},
281
298
});
282
299
```
@@ -308,7 +325,7 @@ It's also possible to pass a function in the `getComponent` prop to lazily evalu
308
325
/>
309
326
```
310
327
311
-
You can use this approach instead of the `component` prop if you want the `ProfileScreen` module to be lazily evaluated when needed. This is especially useful when using [ram bundles](https://reactnative.dev/docs/ram-bundles-inline-requires) to improve initial load.
328
+
You can use this approach instead of the `component` prop if you want the `ProfileScreen` module to be lazily evaluated with [inline requires](https://reactnative.dev/docs/optimizing-javascript-loading#advanced-call-require-inline).
312
329
313
330
#### `children`
314
331
@@ -344,7 +361,7 @@ It takes a function that returns a React element:
Copy file name to clipboardExpand all lines: versioned_docs/version-8.x/navigation-container.md
+10-4Lines changed: 10 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1085,12 +1085,18 @@ function MyComponent() {
1085
1085
### `persistor`
1086
1086
1087
1087
An object containing functions to persist and restore the [navigation state](navigation-state.md).
1088
-
The `persistor` object should contain two functions:
1089
1088
1090
-
-`persist`-Function that receives the navigation state as an argument and should save it to storage.
1091
-
-`restore`-Function that returns the previously saved state from storage, or `undefined`if there's no saved state.
1089
+
The `persistor` object should contain two required functions:
1092
1090
1093
-
These function can be both synchronous or asynchronous. If a promise is returned from the `restore` function, make sure to provide a [`fallback`](navigation-container.md#fallback).
1091
+
-`persist`-Function that receives the serialized navigation state and should save it to storage. The stored state should be removed if it receives `undefined`. It can be synchronous or return a promise.
1092
+
-`restore`-Function that returns the previously saved serialized state, or `undefined`if there's no saved state. It can be synchronous or return a promise.
1093
+
1094
+
By default, the state is serialized with `JSON.stringify` and parsed with `JSON.parse`. You can override this by providing the following optional functions:
1095
+
1096
+
- `stringify` - Function that receives the navigation state and returns a serialized string or `undefined`.
1097
+
- `parse` - Function that receives the serialized string and returns the parsed navigation state or `undefined`.
1098
+
1099
+
If a promise is returned from the `restore` function, make sure to provide a [`fallback`](navigation-container.md#fallback).
1094
1100
1095
1101
See [state persistence guide](state-persistence.md) for example usage.
In this case, `'Dashboard'` refers to the name of a parent screen of `Feed` that's used in the parent drawer navigator.
1798
1798
1799
1799
This approach allows components to not have to know the nesting structure of the navigators. So it's highly recommended to use a screen name when using `getParent`.
1800
1800
1801
-
This method will return `undefined` if there is no matching parent navigator.
1801
+
When called without a screen name, this method returns `undefined` if there is no immediate parent navigator. When called with a screen name, it throws an error if that screen isn't found in the current or parent navigators.
0 commit comments