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
The component passed to `with` receives the `Navigator` component to render as a prop. It accepts any props supported by the navigator.
223
223
224
-
The `screenOptions` and `screenListeners` props passed to the component will be shallow merged with the ones defined in the static config if any.
224
+
To provide dynamic values for individual screen options, you can use the callback for [`screenOptions`](screen-options.md#screenoptions-prop-on-the-navigator) and use `route.name` to return different options for different screens:
225
+
226
+
```js
227
+
constRootStack=createNativeStackNavigator({
228
+
screens: {
229
+
Home: HomeScreen,
230
+
Profile: ProfileScreen,
231
+
},
232
+
}).with(({ Navigator }) => {
233
+
constuser=useUser();
234
+
235
+
return (
236
+
<Navigator
237
+
screenOptions={({ route }) => {
238
+
if (route.name==='Profile') {
239
+
return {
240
+
headerRight:
241
+
user.id===route.params.userId
242
+
? () =><EditButton />
243
+
:undefined,
244
+
};
245
+
}
246
+
247
+
return {};
248
+
}}
249
+
/>
250
+
);
251
+
});
252
+
```
253
+
254
+
Similarly, the [`screenListeners`](navigation-events.md#screenlisteners-prop-on-the-navigator) prop can be used to provide dynamic listeners for the screens:
255
+
256
+
```js
257
+
constRootStack=createNativeStackNavigator({
258
+
screens: {
259
+
Home: HomeScreen,
260
+
Profile: ProfileScreen,
261
+
},
262
+
}).with(({ Navigator }) => {
263
+
constuser=useUser();
264
+
265
+
return (
266
+
<Navigator
267
+
screenListeners={({ route }) => {
268
+
if (route.name==='Profile') {
269
+
return {
270
+
focus: () => {
271
+
console.log(`Profile of user ${route.params.userId} focused`);
272
+
},
273
+
};
274
+
}
275
+
276
+
return {};
277
+
}}
278
+
/>
279
+
);
280
+
});
281
+
```
282
+
283
+
The `screenOptions` and `screenListeners` props passed to the `Navigator` component will be shallow merged with the ones defined in the static config if they are defined in both places.
284
+
285
+
You can also wrap the `Navigator` component in a provider or any additional UI components:
286
+
287
+
```js
288
+
constRootStack=createNativeStackNavigator({
289
+
screens: {
290
+
Home: HomeScreen,
291
+
},
292
+
}).with(({ Navigator }) => {
293
+
return (
294
+
<MyProvider>
295
+
<Navigator/>
296
+
</MyProvider>
297
+
);
298
+
});
299
+
```
300
+
301
+
Here, the `Navigator` component is wrapped inside a `MyProvider` component.
@@ -286,83 +286,214 @@ In the dynamic API, it's possible to wrap the navigator component. For example,
286
286
287
287
In most cases, it can be done in following ways with the static API:
288
288
289
-
### `layout` on parent screen
289
+
### `layout` on the navigator
290
290
291
-
If the navigator is nested inside a screen in another navigator, you can use the [`layout`](screen.md#layout) property on the screen so it wraps the screen's content which includes the navigator:
291
+
If your wrapper doesn't access the navigator's state, you can use the [`layout`](navigator.md#layout) property on the navigator to wrap the navigator's content:
layout: ({ children }) =><SomeProvider>{children}</SomeProvider>,
308
+
screens: {
309
+
Home: HomeScreen,
310
+
},
311
+
});
312
+
```
313
+
314
+
A navigator's layout is rendered as part of the navigator and has access to the navigator's state and options, whereas a wrapper component shown in the dynamic API example does not. So hooks such as [`useNavigationState`](use-navigation-state.md) used in `SomeProvider` will return the parent navigator's state in the dynamic example, but the current navigator's state in the static example.
315
+
316
+
Using `layout` is the recommended approach if your wrapper doesn't need the parent navigator's state. Otherwise, see the next approach.
317
+
318
+
### `with` method on the navigator
319
+
320
+
The [`with`](static-configuration.md#passing-dynamic-props-or-wrapping-the-navigator) method on the navigator returned by `createXNavigator` is the direct equivalent of wrapping the navigator component:
The component passed to `with` receives the `Navigator` component to render as a prop and is equivalent to the navigator component in the dynamic API.
351
+
352
+
## Dynamic navigator props
353
+
354
+
In the dynamic API, you can use hooks inside the navigator component to pass dynamic props to the navigator. In the static API, the [`with`](static-configuration.md#passing-dynamic-props-or-wrapping-the-navigator) method on the navigator lets you do the same:
The component passed to `with` receives the `Navigator` component as a prop. You can pass any props supported by the navigator to the `Navigator` component.
326
395
396
+
To provide dynamic values for individual screen options, you can use the callback for [`screenOptions`](screen-options.md#screenoptions-prop-on-the-navigator) and use `route.name` to return different options for different screens:
console.log(`Profile of user ${route.params.userId} focused`);
463
+
},
464
+
})}
465
+
/>
466
+
</Stack.Navigator>
350
467
);
351
468
}
352
469
```
353
470
354
471
```js title="Static API"
355
472
constRootStack=createNativeStackNavigator({
356
-
layout: ({ children }) =><SomeProvider>{children}</SomeProvider>,
357
473
screens: {
358
474
Home: HomeScreen,
475
+
Profile: ProfileScreen,
359
476
},
477
+
}).with(({ Navigator }) => {
478
+
return (
479
+
<Navigator
480
+
screenListeners={({ route }) => {
481
+
if (route.name==='Profile') {
482
+
return {
483
+
focus: () => {
484
+
console.log(`Profile of user ${route.params.userId} focused`);
485
+
},
486
+
};
487
+
}
488
+
489
+
return {};
490
+
}}
491
+
/>
492
+
);
360
493
});
361
494
```
362
495
363
-
A navigator's layout is rendered as part of the navigator and has access to the navigator's state and options, whereas a wrapper component, or a layout on a parent screen don't.
364
-
365
-
With both approaches, you can access the route params of the parent screen with the [`useRoute`](use-route.md) hook.
496
+
The `screenOptions` and `screenListeners` props passed to the `Navigator` component will be shallow merged with the ones defined in the static config if they are defined in both places.
0 commit comments