Skip to content

Commit 126e792

Browse files
committed
Document alternative way to add wrapper for static vs dynamic
1 parent e377c02 commit 126e792

File tree

2 files changed

+114
-8
lines changed

2 files changed

+114
-8
lines changed

versioned_docs/version-7.x/static-vs-dynamic.md

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,62 @@ In addition, if you were using `navigationKey` to remove or remount screens on a
282282

283283
## Wrappers around navigators
284284

285-
In the dynamic API, it's possible to wrap the navigator component. For example, to add context providers or additional UI around the navigator. Since the navigator configuration is not a component in static API, this is not possible.
285+
In the dynamic API, it's possible to wrap the navigator component. For example, to add context providers or additional UI around the navigator.
286286

287-
However, in many cases, it may be possible to achieve the same result by using the [`layout`](navigator.md#layout) property:
287+
In most cases, it can be done in following ways with the static API:
288+
289+
### `layout` on parent screen
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:
292+
293+
```js title="Dynamic API"
294+
const Tab = createBottomTabNavigator();
295+
296+
function HomeTabs() {
297+
return (
298+
<SomeProvider>
299+
<Tab.Navigator>
300+
<Tab.Screen name="Feed" component={FeedScreen} />
301+
<Tab.Screen name="Notifications" component={NotificationsScreen} />
302+
</Tab.Navigator>
303+
</SomeProvider>
304+
);
305+
}
306+
307+
const Stack = createNativeStackNavigator();
308+
309+
function RootStack() {
310+
return (
311+
<Stack.Navigator>
312+
<Stack.Screen name="Home" component={HomeTabs} />
313+
<Stack.Screen name="Profile" component={ProfileScreen} />
314+
</Stack.Navigator>
315+
);
316+
}
317+
```
318+
319+
```js title="Static API"
320+
const HomeTabs = createBottomTabNavigator({
321+
screens: {
322+
Feed: FeedScreen,
323+
Notifications: NotificationsScreen,
324+
},
325+
});
326+
327+
const RootStack = createNativeStackNavigator({
328+
screens: {
329+
Home: {
330+
screen: HomeTabs,
331+
layout: ({ children }) => <SomeProvider>{children}</SomeProvider>,
332+
},
333+
Profile: ProfileScreen,
334+
},
335+
});
336+
```
337+
338+
### `layout` on the navigator
339+
340+
You can also use the [`layout`](navigator.md#layout) property on the navigator to wrap the navigator's content:
288341

289342
```js title="Dynamic API"
290343
function RootStack() {
@@ -307,9 +360,9 @@ const RootStack = createNativeStackNavigator({
307360
});
308361
```
309362

310-
There are some subtle differences between the two approaches. A layout is rendered as part of the navigator and has access to the navigator's state and options, whereas a wrapper component is outside the navigator.
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.
311364

312-
This is also not possible if the wrapper component used props from the parent component. However, you can still achieve this by moving those props to React context.
365+
With both approaches, you can access the route params of the parent screen with the [`useRoute`](use-route.md) hook.
313366

314367
## Deep linking
315368

versioned_docs/version-8.x/static-vs-dynamic.md

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,62 @@ In addition, if you were using `navigationKey` to remove or remount screens on a
286286

287287
## Wrappers around navigators
288288

289-
In the dynamic API, it's possible to wrap the navigator component. For example, to add context providers or additional UI around the navigator. Since the navigator configuration is not a component in static API, this is not possible.
289+
In the dynamic API, it's possible to wrap the navigator component. For example, to add context providers or additional UI around the navigator.
290290

291-
However, in many cases, it may be possible to achieve the same result by using the [`layout`](navigator.md#layout) property:
291+
In most cases, it can be done in following ways with the static API:
292+
293+
### `layout` on parent screen
294+
295+
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:
296+
297+
```js title="Dynamic API"
298+
const Tab = createBottomTabNavigator();
299+
300+
function HomeTabs() {
301+
return (
302+
<SomeProvider>
303+
<Tab.Navigator>
304+
<Tab.Screen name="Feed" component={FeedScreen} />
305+
<Tab.Screen name="Notifications" component={NotificationsScreen} />
306+
</Tab.Navigator>
307+
</SomeProvider>
308+
);
309+
}
310+
311+
const Stack = createNativeStackNavigator();
312+
313+
function RootStack() {
314+
return (
315+
<Stack.Navigator>
316+
<Stack.Screen name="Home" component={HomeTabs} />
317+
<Stack.Screen name="Profile" component={ProfileScreen} />
318+
</Stack.Navigator>
319+
);
320+
}
321+
```
322+
323+
```js title="Static API"
324+
const HomeTabs = createBottomTabNavigator({
325+
screens: {
326+
Feed: FeedScreen,
327+
Notifications: NotificationsScreen,
328+
},
329+
});
330+
331+
const RootStack = createNativeStackNavigator({
332+
screens: {
333+
Home: createNativeStackScreen({
334+
screen: HomeTabs,
335+
layout: ({ children }) => <SomeProvider>{children}</SomeProvider>,
336+
}),
337+
Profile: ProfileScreen,
338+
},
339+
});
340+
```
341+
342+
### `layout` on the navigator
343+
344+
You can also use the [`layout`](navigator.md#layout) property on the navigator to wrap the navigator's content:
292345

293346
```js title="Dynamic API"
294347
function RootStack() {
@@ -311,9 +364,9 @@ const RootStack = createNativeStackNavigator({
311364
});
312365
```
313366

314-
There are some subtle differences between the two approaches. A layout is rendered as part of the navigator and has access to the navigator's state and options, whereas a wrapper component is outside the navigator.
367+
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.
315368

316-
This is also not possible if the wrapper component used props from the parent component. However, you can still achieve this by moving those props to React context.
369+
With both approaches, you can access the route params of the parent screen with the [`useRoute`](use-route.md) hook.
317370

318371
## Deep linking
319372

0 commit comments

Comments
 (0)