|
| 1 | +--- |
| 2 | +id: screen |
| 3 | +title: Screen |
| 4 | +sidebar_label: Screen |
| 5 | +--- |
| 6 | + |
| 7 | +`Screen` components are used to configure various aspects of screens inside a navigator. |
| 8 | + |
| 9 | +A `Screen` is returned from a `createNavigatorX` function: |
| 10 | + |
| 11 | +```js |
| 12 | +const Stack = createStackNavigator(); // Stack contains Screen & Navigator properties |
| 13 | +``` |
| 14 | + |
| 15 | +After creating the navigator, it can be used as children of the `Navigator` component: |
| 16 | + |
| 17 | +```js |
| 18 | +<Stack.Navigator> |
| 19 | + <Stack.Screen name="Home" component={HomeScreen} /> |
| 20 | + <Stack.Screen name="Profile" component={ProfileScreen} /> |
| 21 | +</Stack.Navigator> |
| 22 | +``` |
| 23 | + |
| 24 | +You need to provide at least a name and a component to render for each screen. |
| 25 | + |
| 26 | +## Props |
| 27 | + |
| 28 | +### `name` |
| 29 | + |
| 30 | +The name to use for the screen. It accepts a string: |
| 31 | + |
| 32 | +```js |
| 33 | +<Stack.Screen name="Profile" component={ProfileScreen} /> |
| 34 | +``` |
| 35 | + |
| 36 | +This name is used to navigate to the screen: |
| 37 | + |
| 38 | +```js |
| 39 | +navigation.navigate('Profile'); |
| 40 | +``` |
| 41 | + |
| 42 | +It is also used for the `name` property in the [`route`](route-prop.md). |
| 43 | + |
| 44 | +While it is supported, we recommend to avoid spaces or special characters in screen names and keep them simple. |
| 45 | + |
| 46 | +### `options` |
| 47 | + |
| 48 | +Options to configure how the screen gets presented in the navigator. It accepts either an object or a function returning an object: |
| 49 | + |
| 50 | +```js |
| 51 | +<Stack.Screen |
| 52 | + name="Profile" |
| 53 | + component={ProfileScreen} |
| 54 | + options={{ |
| 55 | + title: 'Awesome app', |
| 56 | + }} |
| 57 | +/> |
| 58 | +``` |
| 59 | + |
| 60 | +When you pass a function, it'll receive the [`route`](route-prop.md) and [`navigation`](navigation-prop.md): |
| 61 | + |
| 62 | +```js |
| 63 | +<Stack.Screen |
| 64 | + name="Profile" |
| 65 | + component={ProfileScreen} |
| 66 | + options={({ route, navigation }) => ({ |
| 67 | + title: route.params.userId, |
| 68 | + })} |
| 69 | +/> |
| 70 | +``` |
| 71 | + |
| 72 | +See [Options for screens](screen-options.md) for more details and examples. |
| 73 | + |
| 74 | +### `getId` |
| 75 | + |
| 76 | +Callback to return an unique ID to use for the screen. It receives an object with the route params: |
| 77 | + |
| 78 | +```js |
| 79 | +<Stack.Screen |
| 80 | + name="Profile" |
| 81 | + component={ProfileScreen} |
| 82 | + getId={({ params }) => params.userId} |
| 83 | +/> |
| 84 | +``` |
| 85 | + |
| 86 | +When it's specified and doesn't return `undefined`, the unique ID is respected for any navigation. For a given screen name, there will always be only one screen corresponding to an ID. |
| 87 | + |
| 88 | +This is useful for preventing multiple instances of the same screen in the navigator, e.g. - when `params.userId` is used as an ID, subsequent navigation to the screen with the same `userId` will navigate to the existing screen instead of adding a new one to the stack. If the navigation was with a different `userId`, then it'll add a new screen. |
| 89 | + |
| 90 | +### `component` |
| 91 | + |
| 92 | +The React Component to render for the screen: |
| 93 | + |
| 94 | +```js |
| 95 | +<Stack.Screen name="Profile" component={ProfileScreen} /> |
| 96 | +``` |
| 97 | + |
| 98 | +### `getComponent` |
| 99 | + |
| 100 | +Callback to return the React Component to render for the screen: |
| 101 | + |
| 102 | +```js |
| 103 | +<Stack.Screen |
| 104 | + name="Profile" |
| 105 | + getComponent={() => require('./ProfileScreen').default} |
| 106 | +/> |
| 107 | +``` |
| 108 | + |
| 109 | +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. |
| 110 | + |
| 111 | +### `children` |
| 112 | + |
| 113 | +Render callback to return React Element to use for the screen: |
| 114 | + |
| 115 | +```js |
| 116 | +<Stack.Screen name="Profile"> |
| 117 | + {(props) => <ProfileScreen {...props} />} |
| 118 | +</Stack.Screen> |
| 119 | +``` |
| 120 | + |
| 121 | +You can use this approach instead of the `component` prop if you need to pass additional props. Though we recommend using [React context](https://reactjs.org/docs/context.html) for passing data instead. |
| 122 | + |
| 123 | +> Note: By default, React Navigation applies optimizations to screen components to prevent unnecessary renders. Using a render callback removes those optimizations. So if you use a render callback, you'll need to ensure that you use [`React.memo`](https://reactjs.org/docs/react-api.html#reactmemo) or [`React.PureComponent`](https://reactjs.org/docs/react-api.html#reactpurecomponent) for your screen components to avoid performance issues. |
0 commit comments