Skip to content

Commit 7a8a6c2

Browse files
Migrate 'Screen' examples to static API in v7 (#1323)
1 parent c7d5bdc commit 7a8a6c2

File tree

1 file changed

+194
-1
lines changed

1 file changed

+194
-1
lines changed

versioned_docs/version-7.x/screen.md

Lines changed: 194 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,90 @@ title: Screen
44
sidebar_label: Screen
55
---
66

7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
9+
710
`Screen` components are used to configure various aspects of screens inside a navigator.
811

912
A `Screen` is returned from a `createXNavigator` function:
1013

14+
<Tabs groupId="config" queryString="config">
15+
<TabItem value="static" label="Static" default>
16+
17+
```js
18+
const Stack = createNativeStackNavigator({
19+
screens: {
20+
/* content */
21+
},
22+
});
23+
```
24+
25+
</TabItem>
26+
<TabItem value="dynamic" label="Dynamic" default>
27+
1128
```js
1229
const Stack = createNativeStackNavigator(); // Stack contains Screen & Navigator properties
1330
```
1431

32+
</TabItem>
33+
</Tabs>
34+
1535
After creating the navigator, it can be used as children of the `Navigator` component:
1636

37+
<Tabs groupId="config" queryString="config">
38+
<TabItem value="static" label="Static" default>
39+
40+
```js
41+
const Stack = createNativeStackNavigator({
42+
screens: {
43+
Home: HomeScreen,
44+
Profile: ProfileScreen,
45+
},
46+
});
47+
```
48+
49+
</TabItem>
50+
<TabItem value="dynamic" label="Dynamic" default>
51+
1752
```js
1853
<Stack.Navigator>
1954
<Stack.Screen name="Home" component={HomeScreen} />
2055
<Stack.Screen name="Profile" component={ProfileScreen} />
2156
</Stack.Navigator>
2257
```
2358

59+
</TabItem>
60+
</Tabs>
61+
2462
You need to provide at least a name and a component to render for each screen.
2563

2664
## Props
2765

2866
### `name`
2967

30-
The name to use for the screen. It accepts a string:
68+
The name to use for the screen. In dynamic approach it accepts a string:
69+
70+
<Tabs groupId="config" queryString="config">
71+
<TabItem value="static" label="Static" default>
72+
73+
```js
74+
const Stack = createNativeStackNavigator({
75+
screens: {
76+
Profile: ProfileScreen,
77+
},
78+
});
79+
```
80+
81+
</TabItem>
82+
<TabItem value="dynamic" label="Dynamic" default>
3183

3284
```js
3385
<Stack.Screen name="Profile" component={ProfileScreen} />
3486
```
3587

88+
</TabItem>
89+
</Tabs>
90+
3691
This name is used to navigate to the screen:
3792

3893
```js
@@ -47,6 +102,25 @@ While it is supported, we recommend to avoid spaces or special characters in scr
47102

48103
Options to configure how the screen gets presented in the navigator. It accepts either an object or a function returning an object:
49104

105+
<Tabs groupId="config" queryString="config">
106+
<TabItem value="static" label="Static" default>
107+
108+
```js
109+
const Stack = createNativeStackNavigator({
110+
screens: {
111+
Profile: {
112+
screen: ProfileScreen,
113+
options: {
114+
title: 'Awesome app',
115+
},
116+
},
117+
},
118+
});
119+
```
120+
121+
</TabItem>
122+
<TabItem value="dynamic" label="Dynamic" default>
123+
50124
```js
51125
<Stack.Screen
52126
name="Profile"
@@ -57,8 +131,30 @@ Options to configure how the screen gets presented in the navigator. It accepts
57131
/>
58132
```
59133

134+
</TabItem>
135+
</Tabs>
136+
60137
When you pass a function, it'll receive the [`route`](route-object.md) and [`navigation`](navigation-object.md):
61138

139+
<Tabs groupId="config" queryString="config">
140+
<TabItem value="static" label="Static" default>
141+
142+
```js
143+
const Stack = createNativeStackNavigator({
144+
screens: {
145+
Profile: {
146+
screen: ProfileScreen,
147+
options: ({ route, navigation }) => ({
148+
title: route.params.userId,
149+
}),
150+
},
151+
},
152+
});
153+
```
154+
155+
</TabItem>
156+
<TabItem value="dynamic" label="Dynamic" default>
157+
62158
```js
63159
<Stack.Screen
64160
name="Profile"
@@ -69,12 +165,32 @@ When you pass a function, it'll receive the [`route`](route-object.md) and [`nav
69165
/>
70166
```
71167

168+
</TabItem>
169+
</Tabs>
170+
72171
See [Options for screens](screen-options.md) for more details and examples.
73172

74173
### `initialParams`
75174

76175
Initial params to use for the screen. If a screen is used as `initialRouteName`, it'll contain the params from `initialParams`. If you navigate to a new screen, the params passed are shallow merged with the initial params.
77176

177+
<Tabs groupId="config" queryString="config">
178+
<TabItem value="static" label="Static" default>
179+
180+
```js
181+
const Stack = createNativeStackNavigator({
182+
screens: {
183+
Details: {
184+
screen: DetailsScreen,
185+
initialParams: { itemId: 42 },
186+
},
187+
},
188+
});
189+
```
190+
191+
</TabItem>
192+
<TabItem value="dynamic" label="Dynamic" default>
193+
78194
```js
79195
<Stack.Screen
80196
name="Details"
@@ -83,10 +199,30 @@ Initial params to use for the screen. If a screen is used as `initialRouteName`,
83199
/>
84200
```
85201

202+
</TabItem>
203+
</Tabs>
204+
86205
### `getId`
87206

88207
Callback to return an unique ID to use for the screen. It receives an object with the route params:
89208

209+
<Tabs groupId="config" queryString="config">
210+
<TabItem value="static" label="Static" default>
211+
212+
```js
213+
const Stack = createNativeStackNavigator({
214+
screens: {
215+
Profile: {
216+
screen: ProfileScreen,
217+
getId: ({ params }) => params.userId,
218+
},
219+
},
220+
});
221+
```
222+
223+
</TabItem>
224+
<TabItem value="dynamic" label="Dynamic" default>
225+
90226
```js
91227
<Stack.Screen
92228
name="Profile"
@@ -95,6 +231,9 @@ Callback to return an unique ID to use for the screen. It receives an object wit
95231
/>
96232
```
97233

234+
</TabItem>
235+
</Tabs>
236+
98237
By default, `navigate('ScreenName', params)` updates the current screen if the screen name matches, otherwise adds a new screen in a stack navigator. So if you're on `ScreenName` and navigate to `ScreenName` again, it won't add a new screen even if the params are different - it'll update the current screen with the new params instead:
99238

100239
```js
@@ -143,21 +282,55 @@ If `getId` is specified in a tab or drawer navigator, the screen will remount if
143282

144283
The React Component to render for the screen:
145284

285+
<Tabs groupId="config" queryString="config">
286+
<TabItem value="static" label="Static" default>
287+
288+
```js
289+
const Stack = createNativeStackNavigator({
290+
screens: {
291+
Profile: ProfileScreen,
292+
},
293+
});
294+
```
295+
296+
</TabItem>
297+
<TabItem value="dynamic" label="Dynamic" default>
298+
146299
```js
147300
<Stack.Screen name="Profile" component={ProfileScreen} />
148301
```
149302

303+
</TabItem>
304+
</Tabs>
305+
150306
### `getComponent`
151307

152308
Callback to return the React Component to render for the screen:
153309

310+
<Tabs groupId="config" queryString="config">
311+
<TabItem value="static" label="Static" default>
312+
313+
```js
314+
const Stack = createNativeStackNavigator({
315+
screens: {
316+
Profile: () => require('./ProfileScreen').default,
317+
},
318+
});
319+
```
320+
321+
</TabItem>
322+
<TabItem value="dynamic" label="Dynamic" default>
323+
154324
```js
155325
<Stack.Screen
156326
name="Profile"
157327
getComponent={() => require('./ProfileScreen').default}
158328
/>
159329
```
160330

331+
</TabItem>
332+
</Tabs>
333+
161334
You can use this approach instead of the `component` prop if you want the `ProfileScreen` module to be lazily evaluated when needed. This is especially useful when using [ram bundles](https://reactnative.dev/docs/ram-bundles-inline-requires) to improve initial load.
162335

163336
### `children`
@@ -184,6 +357,23 @@ Optional key for this screen. This doesn't need to be unique. If the key changes
184357

185358
This can be useful when we have some screens which we want to be removed or reset when the condition changes:
186359

360+
<Tabs groupId="config" queryString="config">
361+
<TabItem value="static" label="Static" default>
362+
363+
```js
364+
const Stack = createNativeStackNavigator({
365+
screens: {
366+
Profile: {
367+
screen: ProfileScreen,
368+
navigationKey: isSignedIn ? 'user' : 'guest',
369+
},
370+
},
371+
});
372+
```
373+
374+
</TabItem>
375+
<TabItem value="dynamic" label="Dynamic" default>
376+
187377
```js
188378
<Stack.Screen
189379
navigationKey={isSignedIn ? 'user' : 'guest'}
@@ -192,6 +382,9 @@ This can be useful when we have some screens which we want to be removed or rese
192382
/>
193383
```
194384

385+
</TabItem>
386+
</Tabs>
387+
195388
### `listeners`
196389

197390
Event listeners to subscribe to. See [`listeners` prop on `Screen`](navigation-events.md#listeners-prop-on-screen) for more details.

0 commit comments

Comments
 (0)