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,212 @@ 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
+
You can also 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
+
In this case, there is a subtle difference between the two approaches. 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 does not. So hooks such as [`useNavigationState`](use-navigation-state.md) will return the parent navigator's state in the dynamic example, but the current navigator's state in the static example.
315
+
316
+
If your provider doesn't access the navigator's state, then using `layout` is the recommended approach. 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:
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.
393
+
394
+
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`);
461
+
},
462
+
})}
463
+
/>
464
+
</Stack.Navigator>
350
465
);
351
466
}
352
467
```
353
468
354
469
```js title="Static API"
355
470
constRootStack=createNativeStackNavigator({
356
-
layout: ({ children }) =><SomeProvider>{children}</SomeProvider>,
357
471
screens: {
358
472
Home: HomeScreen,
473
+
Profile: ProfileScreen,
359
474
},
475
+
}).with(({ Navigator }) => {
476
+
return (
477
+
<Navigator
478
+
screenListeners={({ route }) => {
479
+
if (route.name==='Profile') {
480
+
return {
481
+
focus: () => {
482
+
console.log(`Profile of user ${route.params.userId} focused`);
483
+
},
484
+
};
485
+
}
486
+
487
+
return {};
488
+
}}
489
+
/>
490
+
);
360
491
});
361
492
```
362
493
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.
494
+
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