1 | # Native Stack Navigator
|
2 |
|
3 | > **_NOTE:_** This README is dedicated for using `native-stack` with React Navigation **v5**. For using `native-stack` in React Navigation **v6** please refer to the [Native Stack Navigator part of React Navigation documentation](https://reactnavigation.org/docs/native-stack-navigator).
|
4 |
|
5 | Provides a way for your app to transition between screens where each new screen is placed on top of a stack.
|
6 |
|
7 | By default the stack navigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, fade in and scale from center on Android. On iOS, the stack navigator can also be configured to a modal style where screens slide in from the bottom.
|
8 |
|
9 | This navigator uses native navigation primitives (`UINavigationController` on iOS and `Fragment` on Android) for navigation under the hood. The main difference from React Navigation's JS-based [stack navigator](https://reactnavigation.org/docs/stack-navigator.html) is that the JS-based navigator re-implements animations and gestures while the native stack navigator relies on the platform primitives for animations and gestures. You should use this navigator if you want native feeling and performance for navigation and don't need much customization, as the customization options of this navigator are limited.
|
10 |
|
11 | ```sh
|
12 | npm install react-native-screens @react-navigation/native
|
13 | ```
|
14 |
|
15 | ## Disabling `react-native-screens`
|
16 |
|
17 | If, for whatever reason, you'd like to disable native screens support and use plain React Native Views add the following code in your entry file (e.g. `App.js`):
|
18 |
|
19 | ```js
|
20 | import { enableScreens } from 'react-native-screens';
|
21 |
|
22 | enableScreens(false);
|
23 | ```
|
24 |
|
25 | ## API Definition
|
26 |
|
27 | To use this navigator, import it from `react-native-screens/native-stack`:
|
28 |
|
29 | ```js
|
30 | import { createNativeStackNavigator } from 'react-native-screens/native-stack';
|
31 |
|
32 | const Stack = createNativeStackNavigator();
|
33 |
|
34 | function MyStack() {
|
35 | return (
|
36 | <Stack.Navigator>
|
37 | <Stack.Screen name="Home" component={Home} />
|
38 | <Stack.Screen name="Notifications" component={Notifications} />
|
39 | <Stack.Screen name="Profile" component={Profile} />
|
40 | <Stack.Screen name="Settings" component={Settings} />
|
41 | </Stack.Navigator>
|
42 | );
|
43 | }
|
44 | ```
|
45 |
|
46 | ### Props
|
47 |
|
48 | The `Stack.Navigator` component accepts following props:
|
49 |
|
50 | #### `initialRouteName`
|
51 |
|
52 | The name of the route to render on the first load of the navigator.
|
53 |
|
54 | #### `screenOptions`
|
55 |
|
56 | Default options to use for the screens in the navigator.
|
57 |
|
58 | ### Options
|
59 |
|
60 | The `options` prop can be used to configure individual screens inside the navigator. Supported options are:
|
61 |
|
62 | #### `backButtonInCustomView`
|
63 |
|
64 | Boolean indicating whether to hide the back button while using `headerLeft` function.
|
65 |
|
66 | #### `contentStyle`
|
67 |
|
68 | Style object for the scene content.
|
69 |
|
70 | #### `customAnimationOnSwipe` (iOS only)
|
71 |
|
72 | Boolean indicating that swipe dismissal should trigger animation provided by `stackAnimation`. Defaults to `false`.
|
73 |
|
74 | #### `direction`
|
75 |
|
76 | String that applies `rtl` or `ltr` form to the stack. On Android, you have to add `android:supportsRtl="true"` in the manifest of your app to enable `rtl`. On Android, if you set the above flag in the manifest, the orientation changes without the need to do it programmatically if the phone has `rtl` direction enabled. On iOS, the direction defaults to `ltr`, and only way to change it is via this prop.
|
77 |
|
78 | #### `disableBackButtonMenu` (iOS only)
|
79 |
|
80 | Boolean indicating whether to show the menu on longPress of iOS >= 14 back button.
|
81 |
|
82 | #### `backButtonDisplayMode` (iOS only)
|
83 |
|
84 | Enum value indicating display mode of **default** back button. It works on iOS >= 14, and is used only when none of: `backTitleFontFamily`, `backTitleFontSize`, `disableBackButtonMenu` or `backTitle` is set. Otherwise, when the button is customized, under the hood we use iOS native `backButtonItem` which overrides `backButtonDisplayMode`. Read more [#2123](https://github.com/software-mansion/react-native-screens/pull/2123). Possible options:
|
85 | - `default` – show given back button previous controller title, system generic or just icon based on available space
|
86 | - `generic` – show given system generic or just icon based on available space
|
87 | - `minimal` – show just an icon
|
88 |
|
89 | #### `fullScreenSwipeEnabled` (iOS only)
|
90 |
|
91 | Boolean indicating whether the swipe gesture should work on whole screen. Swiping with this option results in the same transition animation as `simple_push` by default. It can be changed to other custom animations with `customAnimationOnSwipe` prop, but default iOS swipe animation is not achievable due to usage of custom recognizer. Defaults to `false`.
|
92 |
|
93 | #### `gestureEnabled` (iOS only)
|
94 |
|
95 | Whether you can use gestures to dismiss this screen. Defaults to `true`.
|
96 |
|
97 | #### `gestureResponseDistance` (iOS only)
|
98 |
|
99 | Use it to restrict the distance from the edges of screen in which the gesture should be recognized. To be used alongside `fullScreenSwipeEnabled`. The responsive area is covered with 4 values: `start`, `end`, `top`, `bottom`. Example usage:
|
100 |
|
101 | ```tsx
|
102 | gestureResponseDistance: {
|
103 | start: 200,
|
104 | end: 250,
|
105 | top: 100,
|
106 | bottom: 150,
|
107 | }
|
108 | ```
|
109 |
|
110 | #### `headerBackTitle`
|
111 |
|
112 | Title string used by the back button on iOS. Defaults to the previous scene's `headerTitle` when not set or set to whitespace only value.
|
113 |
|
114 | #### `headerBackTitleStyle`
|
115 |
|
116 | Style object for header back title. Supported properties:
|
117 |
|
118 | - `fontFamily`
|
119 | - `fontSize`
|
120 |
|
121 | #### `headerBackTitleVisible` (iOS only)
|
122 |
|
123 | Whether the back button title should be visible or not. Defaults to `true`.
|
124 |
|
125 | #### `headerCenter`
|
126 |
|
127 | Function which returns a React Element to display in the center of the header.
|
128 |
|
129 | #### `headerHideBackButton`
|
130 |
|
131 | Boolean indicating whether to hide the back button in the header.
|
132 |
|
133 | #### `headerHideShadow`
|
134 |
|
135 | Boolean indicating whether to hide the elevation shadow on the header.
|
136 |
|
137 | #### `headerLargeStyle` (iOS only)
|
138 |
|
139 | Style object for the large header. Supported properties:
|
140 |
|
141 | - `backgroundColor`
|
142 |
|
143 | #### `headerLargeTitle` (iOS only)
|
144 |
|
145 | Boolean used to set a native property to prefer a large title header (like in iOS setting).
|
146 |
|
147 | For the large title to collapse on scroll, the content of the screen should be wrapped in a scrollable view such as `ScrollView` or `FlatList`. If the scrollable area doesn't fill the screen, the large title won't collapse on scroll.
|
148 |
|
149 | #### `headerLargeTitleHideShadow` (iOS only)
|
150 |
|
151 | Boolean that allows for disabling drop shadow under navigation header when the edge of any scrollable content reaches the matching edge of the navigation bar.
|
152 |
|
153 | #### `headerLargeTitleStyle` (iOS only)
|
154 |
|
155 | Style object for header large title. Supported properties:
|
156 |
|
157 | - `fontFamily`
|
158 | - `fontSize`
|
159 | - `color`
|
160 |
|
161 | #### `headerLeft`
|
162 |
|
163 | Function which returns a React Element to display on the left side of the header. For now, on Android, using it will cause the title to also disappear.
|
164 |
|
165 | #### `headerRight`
|
166 |
|
167 | Function which returns a React Element to display on the right side of the header.
|
168 |
|
169 | #### `headerShown`
|
170 |
|
171 | Whether to show or hide the header for the screen. The header is shown by default. Setting this to `false` hides the header.
|
172 |
|
173 | #### `headerStyle`
|
174 |
|
175 | Style object for the header. Supported properties:
|
176 |
|
177 | - `backgroundColor`
|
178 | - `blurEffect` (iOS only).
|
179 |
|
180 | #### `headerTintColor`
|
181 |
|
182 | Tint color for the header. Changes the color of the back button and title.
|
183 |
|
184 | #### `headerTitle`
|
185 |
|
186 | String to be used by the header as title string. Defaults to scene `title`.
|
187 |
|
188 | #### `headerTitleStyle`
|
189 |
|
190 | Style object for header title. Supported properties:
|
191 |
|
192 | - `fontFamily`
|
193 | - `fontSize`
|
194 | - `fontWeight`
|
195 | - `color`
|
196 |
|
197 | #### `headerTopInsetEnabled` (Android only)
|
198 |
|
199 | A Boolean to that lets you opt out of insetting the header. You may want to * set this to `false` if you use an opaque status bar. Defaults to `true`. Insets are always applied on iOS because the header cannot be opaque.
|
200 |
|
201 | #### `headerTranslucent`
|
202 |
|
203 | Boolean indicating whether the navigation bar is translucent.
|
204 |
|
205 | #### `hideKeyboardOnSwipe` (iOS only)
|
206 |
|
207 | Whether the keyboard should hide when swiping to the previous screen. Defaults to `false`.
|
208 |
|
209 | #### `homeIndicatorHidden` (iOS only)
|
210 |
|
211 | Whether the home indicator should be hidden on this screen. Defaults to `false`.
|
212 |
|
213 | #### `nativeBackButtonDismissalEnabled` (Android only)
|
214 |
|
215 | Boolean indicating whether, when the Android default back button is clicked, the `pop` action should be performed on the native side or on the JS side to be able to prevent it.
|
216 | Unfortunately the same behavior is not available on iOS since the behavior of native back button cannot be changed there.
|
217 |
|
218 | Defaults to `false`.
|
219 |
|
220 | #### `navigationBarColor` (Android only)
|
221 |
|
222 | Sets the navigation bar color. Defaults to initial status bar color.
|
223 |
|
224 | #### `navigationBarHidden` (Android only)
|
225 |
|
226 | Sets the visibility of the navigation bar. Defaults to `false`.
|
227 |
|
228 | #### `replaceAnimation`
|
229 |
|
230 | How should the screen replacing another screen animate.
|
231 | The following values are currently supported:
|
232 | - `push` – the new screen will perform push animation.
|
233 | - `pop` – the new screen will perform pop animation.
|
234 |
|
235 | Defaults to `pop`.
|
236 |
|
237 | #### `sheetAllowedDetents` (iOS only)
|
238 |
|
239 | Describes heights where a sheet can rest.
|
240 | Works only when `stackPresentation` is set to `formSheet`.
|
241 |
|
242 | Available values:
|
243 |
|
244 | - `large` - only large detent level will be allowed
|
245 | - `medium` - only medium detent level will be allowed
|
246 | - `all` - all detent levels will be allowed
|
247 |
|
248 | Defaults to `large`.
|
249 |
|
250 | #### `sheetExpandsWhenScrolledToEdge` (iOS only)
|
251 |
|
252 | Whether the sheet should expand to larger detent when scrolling.
|
253 | Works only when `stackPresentation` is set to `formSheet`.
|
254 |
|
255 | Defaults to `true`.
|
256 |
|
257 | #### `sheetCornerRadius (iOS only)
|
258 |
|
259 | The corner radius that the sheet will try to render with.
|
260 | Works only when `stackPresentation` is set to `formSheet`.
|
261 |
|
262 | If set to non-negative value it will try to render sheet with provided radius, else it will apply system default.
|
263 |
|
264 | Defaults to system default.
|
265 |
|
266 | #### `sheetGrabberVisible` (iOS only)
|
267 |
|
268 | Boolean indicating whether the sheet shows a grabber at the top.
|
269 | Works only when `stackPresentation` is set to `formSheet`.
|
270 | Defaults to `false`.
|
271 |
|
272 | #### `sheetLargestUndimmedDetent` (iOS only)
|
273 |
|
274 | The largest sheet detent for which a view underneath won't be dimmed.
|
275 | Works only when `stackPresentation` is set to `formSheet`.
|
276 |
|
277 | If this prop is set to:
|
278 |
|
279 | - `large` - the view underneath won't be dimmed at any detent level
|
280 | - `medium` - the view underneath will be dimmed only when detent level is `large`
|
281 | - `all` - the view underneath will be dimmed for any detent level
|
282 |
|
283 | Defaults to `all`.
|
284 |
|
285 | #### `stackAnimation`
|
286 |
|
287 | How the given screen should appear/disappear when pushed or popped at the top of the stack. Possible values:
|
288 |
|
289 | - `default` - uses a platform default animation
|
290 | - `fade` - fades screen in or out.
|
291 | - `fade_from_bottom` – performs a fade from bottom animation
|
292 | - `flip` – flips the screen, requires stackPresentation: `modal` (iOS only)
|
293 | - `simple_push` – performs a default animation, but without shadow and native header transition (iOS only)
|
294 | - `slide_from_bottom` – performs a slide from bottom animation
|
295 | - `slide_from_right` - slide in the new screen from right to left (Android only, resolves to default transition on iOS)
|
296 | - `slide_from_left` - slide in the new screen from left to right
|
297 | - `ios` - iOS like slide in animation (Android only, resolves to default transition on iOS)
|
298 | - `none` - the screen appears/disappears without an animation.
|
299 |
|
300 | Defaults to `default`.
|
301 |
|
302 | #### `stackPresentation`
|
303 |
|
304 | How the screen should be presented. Possible values:
|
305 |
|
306 | - `push` - The new screen will be pushed onto a stack. The default animation on iOS is to slide from the side. The animation on Android may vary depending on the OS version and theme.
|
307 | - `modal` - The new screen will be presented modally. In addition, this allows for a nested stack to be rendered inside such screens.
|
308 | - `transparentModal` - The new screen will be presented modally. In addition, the second to last screen will remain attached to the stack container such that if the top screen is translucent, the content below can still be seen. If `"modal"` is used instead, the below screen gets removed as soon as the transition ends.
|
309 | - `containedModal` – will use "UIModalPresentationCurrentContext" modal style on iOS and will fallback to `"modal"` on Android.
|
310 | - `containedTransparentModal` – will use "UIModalPresentationOverCurrentContext" modal style on iOS and will fallback to `"transparentModal"` on Android.
|
311 | - `fullScreenModal` – will use "UIModalPresentationFullScreen" modal style on iOS and will fallback to `"modal"` on Android.
|
312 | - `formSheet` – will use "UIModalPresentationFormSheet" modal style on iOS and will fallback to `"modal"` on Android.
|
313 |
|
314 | Defaults to `push`.
|
315 |
|
316 | Using `containedModal` and `containedTransparentModal` with other types of modals in one native stack navigator is not recommended and can result in a freeze or a crash of the application.
|
317 |
|
318 | #### `swipeDirection` (iOS only)
|
319 |
|
320 | Sets the direction in which you should swipe to dismiss the screen. The following values are supported:
|
321 | - `vertical` – dismiss screen vertically
|
322 | - `horizontal` – dismiss screen horizontally (default)
|
323 |
|
324 | When using `vertical` option, options `fullScreenSwipeEnabled: true`, `customAnimationOnSwipe: true` and `stackAnimation: 'slide_from_bottom'` are set by default.
|
325 |
|
326 | #### `title`
|
327 |
|
328 | A string that can be used as a fallback for `headerTitle`.
|
329 |
|
330 | #### `transitionDuration` (iOS only)
|
331 |
|
332 | Changes the duration (in milliseconds) of `slide_from_bottom`, `fade_from_bottom`, `fade` and `simple_push` transitions on iOS. Defaults to `350`.
|
333 |
|
334 | The duration of `default` and `flip` transitions isn't customizable.
|
335 |
|
336 | ### freezeOnBlur
|
337 |
|
338 | Whether inactive screens should be suspended from re-rendering.
|
339 |
|
340 | Defaults to `false`. When `enableFreeze()` is run at the top of the application defaults to `true`.
|
341 |
|
342 | #### `useTransitionProgress`
|
343 |
|
344 | Hook providing context value of transition progress of the current screen to be used with `react-native` `Animated`. It consists of 2 values:
|
345 | - `progress` - `Animated.Value` between `0.0` and `1.0` with the progress of the current transition.
|
346 | - `closing` - `Animated.Value` of `1` or `0` indicating if the current screen is being navigated into or from.
|
347 | - `goingForward` - `Animated.Value` of `1` or `0` indicating if the current transition is pushing or removing screens.
|
348 |
|
349 | ```jsx
|
350 | import {Animated} from 'react-native';
|
351 | import {useTransitionProgress} from 'react-native-screens';
|
352 |
|
353 | function Home() {
|
354 | const {progress} = useTransitionProgress();
|
355 |
|
356 | const opacity = progress.interpolate({
|
357 | inputRange: [0, 0.5, 1],
|
358 | outputRange: [1.0, 0.0 ,1.0],
|
359 | extrapolate: 'clamp',
|
360 | });
|
361 |
|
362 | return (
|
363 | <Animated.View style={{opacity, height: 50, width: '100%', backgroundColor: 'green'}} />
|
364 | );
|
365 | }
|
366 | ```
|
367 |
|
368 | #### `useReanimatedTransitionProgress`
|
369 |
|
370 | A callback called every frame during the transition of screens to be used with `react-native-reanimated` version `2.x`. It consists of 2 shared values:
|
371 | - `progress` - between `0.0` and `1.0` with the progress of the current transition.
|
372 | - `closing` - `1` or `0` indicating if the current screen is being navigated into or from.
|
373 | - `goingForward` - `1` or `0` indicating if the current transition is pushing or removing screens.
|
374 |
|
375 | In order to use it, you need to have `react-native-reanimated` version `2.x` installed in your project and wrap your code with `ReanimatedScreenProvider`, like this:
|
376 |
|
377 | ```jsx
|
378 | import {ReanimatedScreenProvider} from 'react-native-screens/reanimated';
|
379 |
|
380 | export default function App() {
|
381 | return (
|
382 | <ReanimatedScreenProvider>
|
383 | <YourApp />
|
384 | </ReanimatedScreenProvider>
|
385 | );
|
386 | }
|
387 | ```
|
388 |
|
389 | Then you can use `useReanimatedTransitionProgress` to get the shared values:
|
390 |
|
391 | ```jsx
|
392 | import {useReanimatedTransitionProgress} from 'react-native-screens/reanimated';
|
393 | import Animated, {useAnimatedStyle, useDerivedValue} from 'react-native-reanimated';
|
394 |
|
395 | function Home() {
|
396 | const reaProgress = useReanimatedTransitionProgress();
|
397 | const sv = useDerivedValue(() => (reaProgress.progress.value < 0.5 ? (reaProgress.progress.value * 50) : ((1 - reaProgress.progress.value) * 50)) + 50);
|
398 | const reaStyle = useAnimatedStyle(() => {
|
399 | return {
|
400 | width: sv.value,
|
401 | height: sv.value,
|
402 | backgroundColor: 'blue',
|
403 | };
|
404 | });
|
405 |
|
406 | return (
|
407 | <Animated.View style={reaStyle} />
|
408 | );
|
409 | }
|
410 | ```
|
411 |
|
412 | ### Status bar and orientation managment
|
413 |
|
414 | With `native-stack`, the status bar and screen orientation can be managed by `UIViewController` on iOS. On Android, the status bar and screen orientation can be managed by `FragmentActivity`. On iOS, it requires:
|
415 |
|
416 | 1. For status bar managment: enabling (or deleting) `View controller-based status bar appearance` in your Info.plist file (it disables the option to use React Native's `StatusBar` component).
|
417 | 2. For both status bar and orientation managment: adding `#import <RNScreens/UIViewController+RNScreens.h>` in your project's `AppDelegate.m` (you can see this change applied in the `AppDelegate.m` of `Example` project).
|
418 |
|
419 | On Android, no additional setup is required, although, you should keep in mind that once you set the orientation or status bar props, `react-native-screens` will manage them on every screen, so you shouldn't use other methods of manipulating them then.
|
420 | #### `screenOrientation`
|
421 |
|
422 | Sets the current screen's available orientations and forces rotation if current orientation is not included. On iOS, if you have supported orientations set in `info.plist`, they will take precedence over this prop. Possible values:
|
423 |
|
424 | - `default` - on iOS, it resolves to [UIInterfaceOrientationMaskAllButUpsideDown](https://developer.apple.com/documentation/uikit/uiinterfaceorientationmask/uiinterfaceorientationmaskallbutupsidedown?language=objc). On Android, this lets the system decide the best orientation.
|
425 | - `all`
|
426 | - `portrait`
|
427 | - `portrait_up`
|
428 | - `portrait_down`
|
429 | - `landscape`
|
430 | - `landscape_left`
|
431 | - `landscape_right`
|
432 |
|
433 | Defaults to `default`.
|
434 |
|
435 | #### `statusBarAnimation`
|
436 |
|
437 | Sets the status bar animation (similar to the `StatusBar` component). Possible values: `fade`, `none`, `slide`. On Android, this prop considers the transition of changing status bar color (see https://reactnative.dev/docs/statusbar#animated). There will be no animation if `none` provided.
|
438 |
|
439 | Defaults to `fade` on iOS and `none` on Android.
|
440 |
|
441 | #### `statusBarColor` (Android only)
|
442 |
|
443 | Sets the status bar color (similar to the `StatusBar` component). Defaults to initial status bar color.
|
444 |
|
445 | #### `statusBarHidden`
|
446 |
|
447 | Boolean saying if the status bar for this screen is hidden.
|
448 |
|
449 | Defaults to `false`.
|
450 |
|
451 | #### `statusBarStyle`
|
452 |
|
453 | Sets the status bar color (similar to the `StatusBar` component). On iOS, the possible values are: `auto` (based on [user interface style](https://developer.apple.com/documentation/uikit/uiuserinterfacestyle?language=objc), `inverted` (colors opposite to `auto`), `light`, `dark`. On Android, the status bar will be dark if set to `dark` and `light` otherwise.
|
454 |
|
455 | Defaults to `auto`.
|
456 |
|
457 | #### `statusBarTranslucent` (Android only)
|
458 |
|
459 | Sets the translucency of the status bar (similar to the `StatusBar` component). Defaults to `false`.
|
460 |
|
461 | ### Search bar
|
462 |
|
463 | The search bar is just a `searchBar` property that can be specified in the navigator's `screenOptions` or an individual screen's `options`. Search bars are rarely static so normally it is controlled by passing an object to `searchBar` navigation option in the component's body.
|
464 |
|
465 | Example:
|
466 |
|
467 | ```js
|
468 | React.useLayoutEffect(() => {
|
469 | navigation.setOptions({
|
470 | searchBar: {
|
471 | // search bar options
|
472 | }
|
473 | });
|
474 | }, [navigation]);
|
475 | ```
|
476 |
|
477 | We advise using `useLayoutEffect` hook instead of `useEffect` when managing `searchBar` props to avoid unexpected layout issues.
|
478 |
|
479 | Supported properties are described below.
|
480 |
|
481 | #### `autoCapitalize`
|
482 |
|
483 | Controls whether the text is automatically auto-capitalized as it is entered by the user.
|
484 | Possible values:
|
485 |
|
486 | - `none`
|
487 | - `words`
|
488 | - `sentences`
|
489 | - `characters`
|
490 |
|
491 | Defaults to `sentences` on iOS and `'none'` on Android.
|
492 |
|
493 | #### `autoFocus` (Android only)
|
494 |
|
495 | When set to `true` focuses search bar automatically when screen is appearing. Default value is `false`.
|
496 |
|
497 | #### `barTintColor`
|
498 |
|
499 | The search field background color.
|
500 |
|
501 | By default bar tint color is translucent.
|
502 |
|
503 | #### `tintColor` (iOS only)
|
504 |
|
505 | The color for the cursor caret and cancel button text.
|
506 |
|
507 | #### `cancelButtonText` (iOS only)
|
508 |
|
509 | The text to be used instead of default `Cancel` button text.
|
510 |
|
511 | #### `disableBackButtonOverride` (Android only)
|
512 |
|
513 | Default behavior is to prevent screen from going back when search bar is open (`disableBackButtonOverride: false`). If you don't want this to happen set `disableBackButtonOverride` to `true`
|
514 |
|
515 | #### `hideNavigationBar` (iOS only)
|
516 |
|
517 | Boolean indicating whether to hide the navigation bar during searching.
|
518 |
|
519 | Defaults to `true`.
|
520 |
|
521 | #### `hideWhenScrolling` (iOS only)
|
522 |
|
523 | Boolean indicating whether to hide the search bar when scrolling.
|
524 |
|
525 | Defaults to `true`.
|
526 |
|
527 | #### `inputType` (Android only)
|
528 |
|
529 | This prop is used to change type of the input and keyboard. Default value is `'text'`.
|
530 |
|
531 | All values:
|
532 | - `'text'` - normal text input
|
533 | - `'number'` - number input
|
534 | - `'email'` - email input
|
535 | - `'phone'` - phone input
|
536 |
|
537 | #### `obscureBackground` (iOS only)
|
538 |
|
539 | Boolean indicating whether to obscure the underlying content with semi-transparent overlay.
|
540 |
|
541 | Defaults to `true`.
|
542 |
|
543 | #### `onBlur`
|
544 |
|
545 | A callback that gets called when search bar has lost focus.
|
546 |
|
547 | #### `onCancelButtonPress`
|
548 |
|
549 | A callback that gets called when the cancel button is pressed.
|
550 |
|
551 | #### `onChangeText`
|
552 |
|
553 | A callback that gets called when the text changes. It receives the current text value of the search bar.
|
554 |
|
555 | Example:
|
556 |
|
557 | ```js
|
558 | const [search, setSearch] = React.useState('');
|
559 |
|
560 | React.useLayoutEffect(() => {
|
561 | navigation.setOptions({
|
562 | searchBar: {
|
563 | onChangeText: (event) => setSearch(event.nativeEvent.text),
|
564 | }
|
565 | });
|
566 | }, [navigation]);
|
567 | ```
|
568 | #### `onClose` (Android only)
|
569 |
|
570 | A callback that gets called when search bar is closing
|
571 |
|
572 | #### `onFocus`
|
573 |
|
574 | A callback that gets called when search bar has received focus.
|
575 |
|
576 | #### `onOpen` (Android only)
|
577 |
|
578 | A callback that gets called when search bar is expanding
|
579 |
|
580 | #### `onSearchButtonPress`
|
581 |
|
582 | A callback that gets called when the search button is pressed. It receives the current text value of the search bar.
|
583 |
|
584 | #### `placeholder`
|
585 |
|
586 | Text displayed when search field is empty.
|
587 |
|
588 | Defaults to an empty string.
|
589 |
|
590 | #### `placement` (iOS only)
|
591 |
|
592 | Position of the search bar
|
593 |
|
594 | Supported values:
|
595 |
|
596 | * `automatic` - the search bar is placed according to current layout
|
597 | * `inline` - the search bar is placed on the trailing edge of navigation bar
|
598 | * `stacked` - the search bar is placed below the other content in navigation bar
|
599 |
|
600 | Defaults to `stacked`
|
601 |
|
602 | #### `textColor`
|
603 |
|
604 | The search field text color.
|
605 |
|
606 | #### `hintTextColor`
|
607 |
|
608 | The search hint text color. (Android only)
|
609 |
|
610 | #### `headerIconColor`
|
611 |
|
612 | The search and close icon color shown in the header. (Android only)
|
613 |
|
614 | #### `shouldShowHintSearchIcon`
|
615 |
|
616 | Show the search hint icon when search bar is focused. (Android only)
|
617 |
|
618 | #### `ref`
|
619 |
|
620 | A React ref to imperatively modify search bar. Supported actions:
|
621 |
|
622 | * `focus` - focus on search bar
|
623 | * `blur` - remove focus from search bar
|
624 | * `clearText` - clear text in search bar
|
625 | * `setText` - set search bar's content to given string
|
626 | * `cancelSearch` - cancel search in search bar.
|
627 | * `toggleCancelButton` (iOS only) - toggle cancel button display near search bar.
|
628 |
|
629 | ### Events
|
630 |
|
631 | The navigator can [emit events](https://reactnavigation.org/docs/navigation-events) on certain actions. Supported events are:
|
632 |
|
633 | #### `appear` - deprecated
|
634 |
|
635 | Use `transitionEnd` event with `data.closing: false` instead.
|
636 |
|
637 | Event which fires when the screen appears.
|
638 |
|
639 | Example:
|
640 |
|
641 | ```js
|
642 | React.useEffect(
|
643 | () => {
|
644 | const unsubscribe = navigation.addListener('appear', e => {
|
645 | // Do something
|
646 | });
|
647 |
|
648 | return unsubscribe;
|
649 | },
|
650 | [navigation]
|
651 | );
|
652 | ```
|
653 |
|
654 | #### `dismiss`
|
655 |
|
656 | Event which fires when the current screen is dismissed by hardware back (on Android) or dismiss gesture (swipe back or down).
|
657 |
|
658 | Example:
|
659 |
|
660 | ```js
|
661 | React.useEffect(
|
662 | () => {
|
663 | const unsubscribe = navigation.addListener('dismiss', e => {
|
664 | // Do something
|
665 | });
|
666 |
|
667 | return unsubscribe;
|
668 | },
|
669 | [navigation]
|
670 | );
|
671 | ```
|
672 |
|
673 | #### `transitionStart`
|
674 |
|
675 | Event which fires when a transition animation starts.
|
676 |
|
677 | Event data:
|
678 |
|
679 | - `closing` - Whether the screen will be dismissed or will appear.
|
680 |
|
681 | Example:
|
682 |
|
683 | ```js
|
684 | React.useEffect(
|
685 | () => {
|
686 | const unsubscribe = navigation.addListener('transitionStart', e => {
|
687 | if (e.data.closing) {
|
688 | // Will be dismissed
|
689 | } else {
|
690 | // Will appear
|
691 | }
|
692 | });
|
693 |
|
694 | return unsubscribe;
|
695 | },
|
696 | [navigation]
|
697 | );
|
698 | ```
|
699 |
|
700 | #### `transitionEnd`
|
701 |
|
702 | Event which fires when a transition animation ends.
|
703 |
|
704 | Event data:
|
705 |
|
706 | - `closing` - Whether the screen was dismissed or did appear.
|
707 |
|
708 | Example:
|
709 |
|
710 | ```js
|
711 | React.useEffect(
|
712 | () => {
|
713 | const unsubscribe = navigation.addListener('transitionEnd', e => {
|
714 | if (e.data.closing) {
|
715 | // Was dismissed
|
716 | } else {
|
717 | // Did appear
|
718 | }
|
719 | });
|
720 |
|
721 | return unsubscribe;
|
722 | },
|
723 | [navigation]
|
724 | );
|
725 | ```
|
726 |
|
727 | ### Helpers
|
728 |
|
729 | The stack navigator adds the following methods to the navigation prop:
|
730 |
|
731 | #### `push`
|
732 |
|
733 | Pushes a new screen to the top of the stack and navigate to it. The method accepts the following arguments:
|
734 |
|
735 | - `name` - _string_ - Name of the route to push onto the stack.
|
736 | - `params` - _object_ - Screen params to merge into the destination route (found in the pushed screen through `route.params`).
|
737 |
|
738 | ```js
|
739 | navigation.push('Profile', { name: 'Michaś' });
|
740 | ```
|
741 |
|
742 | #### `pop`
|
743 |
|
744 | Pops the current screen from the stack and navigates back to the previous screen. It takes one optional argument (`count`), which allows you to specify how many screens to pop back by.
|
745 |
|
746 | ```js
|
747 | navigation.pop();
|
748 | ```
|
749 |
|
750 | #### `popToTop`
|
751 |
|
752 | Pops all of the screens in the stack except the first one and navigates to it.
|
753 |
|
754 | ```js
|
755 | navigation.popToTop();
|
756 | ```
|
757 |
|
758 | ## Additional options
|
759 |
|
760 | ### Measuring header's height
|
761 |
|
762 | To measure header's height, you can use the `useHeaderHeight`, `useAnimatedHeaderHeight` or `useReanimatedHeaderHeight` hook.
|
763 | - `useHeaderHeight` returns the static header's height. The value provided by this hook changes when screen appears, when there's a change in header options or screen orientation.
|
764 | Use this hook if you're sure your header height won't change dynamically, or when the screen is heavy.
|
765 | - `useAnimatedHeaderHeight` dynamically calculates the header's height. The value provided by this hook changes with every view layout (such as shrinking a large header into small one with a ScrollView). It returns an Animated.Value.
|
766 | Please beware of using this hook in heavy components, as it may result in performance issues.
|
767 | - `useReanimatedHeaderHeight` also dynamically calculates the header's height but uses React Native Reanimated under the hood. It returns an Animated.SharedValue.
|
768 | Make sure to wrap your Stack.Navigator with `ReanimatedScreenProvider` before using this hook.
|
769 |
|
770 | We recommend using `useReanimatedHeaderHeight` rather than `useAnimatedHeaderHeight`. See [Shared Values vs Animated.Value](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/shared-values/#shared-values-vs-animatedvalue) section in React Native Reanimated's documentation for full comparison.
|
771 |
|
772 | ```tsx
|
773 | // for using useHeaderHeight
|
774 | import {useHeaderHeight} from 'react-native-screens/native-stack';
|
775 |
|
776 | // for using useAnimatedHeaderHeight
|
777 | import {useAnimatedHeaderHeight} from 'react-native-screens/native-stack';
|
778 |
|
779 | // for using useReanimatedHeaderHeight
|
780 | import {useReanimatedHeaderHeight} from 'react-native-screens/reanimated';
|
781 | ```
|
782 |
|
783 | ## Example
|
784 |
|
785 | ```js
|
786 | import { createNativeStackNavigator } from 'react-native-screens/native-stack';
|
787 |
|
788 | const Stack = createNativeStackNavigator();
|
789 |
|
790 | function MyStack() {
|
791 | return (
|
792 | <Stack.Navigator
|
793 | initialRouteName="Home"
|
794 | screenOptions={{
|
795 | headerShown: false,
|
796 | headerTintColor: 'white',
|
797 | headerStyle: { backgroundColor: 'tomato' },
|
798 | }}>
|
799 | <Stack.Screen
|
800 | name="Home"
|
801 | component={Home}
|
802 | options={{
|
803 | title: 'Awesome app',
|
804 | }}
|
805 | />
|
806 | <Stack.Screen
|
807 | name="Profile"
|
808 | component={Profile}
|
809 | options={{
|
810 | title: 'My profile',
|
811 | }}
|
812 | />
|
813 | <Stack.Screen
|
814 | name="Settings"
|
815 | component={Settings}
|
816 | options={{
|
817 | gestureEnabled: false,
|
818 | }}
|
819 | />
|
820 | </Stack.Navigator>
|
821 | );
|
822 | }
|
823 | ```
|