Skip to content

Commit e321de1

Browse files
committed
Update combining static and dynamic APIs
1 parent 8d369e7 commit e321de1

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

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

+94-1
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ const RootStack = createNativeStackNavigator({
7575
screen: FeedScreen,
7676
linking: {
7777
path: 'feed',
78+
// highlight-start
7879
screens: {
7980
Latest: 'latest',
8081
Popular: 'popular',
8182
},
83+
// highlight-end
8284
},
8385
},
8486
},
@@ -100,8 +102,10 @@ type FeedParamList = {
100102
Popular: undefined;
101103
};
102104

105+
// highlight-next-line
103106
type Props = StaticScreenProps<NavigatorScreenParams<FeedParamList>>;
104107

108+
// highlight-next-line
105109
function FeedScreen(_: Props) {
106110
// ...
107111
}
@@ -119,4 +123,93 @@ This is based on how we'd define the type for a screen with a nested navigator w
119123

120124
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.
121125

122-
TODO
126+
Let's consider the following example:
127+
128+
- You have a root stack navigator that contains a tab navigator in a screen.
129+
- The root stack navigator is defined using the dynamic API.
130+
131+
Our dynamic configuration would look like this:
132+
133+
```js
134+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
135+
136+
const RootStack = createNativeStackNavigator();
137+
138+
function RootStackScreen() {
139+
return (
140+
<RootStack.Navigator>
141+
<RootStack.Screen name="Home" component={HomeScreen} />
142+
<RootStack.Screen name="Feed" component={FeedScreen} />
143+
</RootStack.Navigator>
144+
);
145+
}
146+
```
147+
148+
Here, `FeedScreen` is a component that renders a tab navigator and is defined using the static API:
149+
150+
```js
151+
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
152+
153+
const FeedTabs = createBottomTabNavigator({
154+
screens: {
155+
Latest: {
156+
screen: LatestScreen,
157+
},
158+
Popular: {
159+
screen: PopularScreen,
160+
},
161+
},
162+
});
163+
```
164+
165+
To use the `FeedTabs` navigator for the `Feed` screen, we need to use the `createComponentForStaticNavigation` function:
166+
167+
```js
168+
import { createComponentForStaticNavigation } from '@react-navigation/native';
169+
170+
// highlight-next-line
171+
const FeedScreen = createComponentForStaticNavigation(FeedTabs, 'Feed');
172+
```
173+
174+
In addition, we can generate the TypeScript types for the `FeedTabs` navigator and use it in the types of `RootStack` without needing to write them manually:
175+
176+
```tsx
177+
import {
178+
StaticParamList,
179+
NavigatorScreenParams,
180+
} from '@react-navigation/native';
181+
182+
// highlight-next-line
183+
type FeedTabsParamList = StaticParamList<typeof FeedTabs>;
184+
185+
type RootStackParamList = {
186+
Home: undefined;
187+
// highlight-next-line
188+
Feed: NavigatorScreenParams<FeedTabsParamList>;
189+
};
190+
```
191+
192+
Similarly, we can generate the linking configuration for the `FeedTabs` navigator and use it in the linking configuration passed to `NavigationContainer`:
193+
194+
```js
195+
import { createPathConfigForStaticNavigation } from '@react-navigation/native';
196+
197+
// highlight-next-line
198+
const feedScreens = createPathConfigForStaticNavigation(FeedTabs);
199+
200+
const linking = {
201+
prefixes: ['https://mychat.com', 'mychat://'],
202+
config: {
203+
screens: {
204+
Home: '',
205+
Feed: {
206+
path: 'feed',
207+
// highlight-next-line
208+
screens: feedScreens,
209+
},
210+
},
211+
},
212+
};
213+
```
214+
215+
This will generate the linking configuration for the `Feed` screen based on the configuration of the `FeedTabs` navigator.

0 commit comments

Comments
 (0)