Skip to content

Commit 751899a

Browse files
committed
Document how to nest dynamic navigator inside static
1 parent 99d242d commit 751899a

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

versioned_docs/version-7.x/static-combine-with-dynamic.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,120 @@ id: static-combine-with-dynamic
33
title: Combining static and dynamic APIs
44
sidebar_label: Combining with dynamic API
55
---
6+
7+
While the static API has many advantages, it doesn't fit use cases where the navigation configuration needs to be dynamic. So React Navigation supports interop between the static and dynamic APIs.
8+
9+
Keep in mind that the features provided by the static API such as automatic linking configuration and automatic TypeScript types need the whole configuration to be static. If part of the configuration is dynamic, you'll need to handle those parts manually.
10+
11+
There are 2 ways you may want to combine the static and dynamic APIs:
12+
13+
## Static root navigator, dynamic nested navigator
14+
15+
This is useful if you want to keep your configuration static, but need to use a dynamic configuration for a specific navigator.
16+
17+
Let's consider the following example:
18+
19+
- You have a root stack navigator that contains a tab navigator in a screen.
20+
- The tab navigator is defined using the dynamic API.
21+
22+
Our static configuration would look like this:
23+
24+
```js
25+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
26+
27+
const RootStack = createNativeStackNavigator({
28+
screens: {
29+
Home: {
30+
screen: HomeScreen,
31+
},
32+
Feed: {
33+
screen: FeedScreen,
34+
linking: {
35+
path: 'feed',
36+
},
37+
},
38+
},
39+
});
40+
```
41+
42+
Here, `FeedScreen` is a component that renders a tab navigator and is defined using the dynamic API:
43+
44+
```js
45+
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
46+
47+
const Tab = createBottomTabNavigator();
48+
49+
function FeedScreen() {
50+
return (
51+
<Tab.Navigator>
52+
<Tab.Screen name="Latest" component={LatestScreen} />
53+
<Tab.Screen name="Popular" component={PopularScreen} />
54+
</Tab.Navigator>
55+
);
56+
}
57+
```
58+
59+
This code will work, but we're missing 2 things:
60+
61+
- Linking configuration for the screens in the top tab navigator.
62+
- TypeScript types for the screens in the top tab navigator.
63+
64+
Since the nested navigator is defined using the dynamic API, we need to handle these manually. For the linking configuration, we can define the screens in the `linking` property of the `Feed` screen:
65+
66+
```js
67+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
68+
69+
const RootStack = createNativeStackNavigator({
70+
screens: {
71+
Home: {
72+
screen: HomeScreen,
73+
},
74+
Feed: {
75+
screen: FeedScreen,
76+
linking: {
77+
path: 'feed',
78+
screens: {
79+
Latest: 'latest',
80+
Popular: 'popular',
81+
},
82+
},
83+
},
84+
},
85+
});
86+
```
87+
88+
Here the `screens` property is the same as how you'd define it with `linking` config with the dynamic API. It can contain configuration for any nested navigators as well. See [configuring links](configuring-links.md) for more details on the API.
89+
90+
For the TypeScript types, we can define the type of the `FeedScreen` component:
91+
92+
```tsx
93+
import {
94+
StaticScreenProps,
95+
NavigatorScreenParams,
96+
} from '@react-navigation/native';
97+
98+
type FeedParamList = {
99+
Latest: undefined;
100+
Popular: undefined;
101+
};
102+
103+
type Props = StaticScreenProps<NavigatorScreenParams<FeedParamList>>;
104+
105+
function FeedScreen(_: Props) {
106+
// ...
107+
}
108+
```
109+
110+
In the above snippet:
111+
112+
1. We first define the param list type for screens in the navigator that defines params for each screen
113+
2. Then we use the `NavigatorScreenParams` type to get the type of route's `params` which will include types for the nested screens
114+
3. Finally, we use the type of `params` with `StaticScreenProps` to define the type of the screen component
115+
116+
This is based on how we'd define the type for a screen with a nested navigator with the dynamic API. See [Type checking screens and params in nested navigator](typescript.md#type-checking-screens-and-params-in-nested-navigator).
117+
118+
## Dynamic root navigator, static nested navigator
119+
120+
This is useful if you already have a dynamic configuration, but want to migrate to the static API. This way you can migrate one navigator at a time.
121+
122+
TODO

0 commit comments

Comments
 (0)