You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a screen is marked to be retained, actions such as [`goBack`](navigation-actions.md#goback), [`pop`](#pop), [`popToTop`](#poptotop), [`replace`](#replace) etc. will remove it from history, but keep it in the navigation state. So the screen is not unmounted and stays rendered in the background, preserving its local state. Similar to [preloaded routes](navigation-actions.md#preload), it can be brought to focus with `navigate`.
587
+
588
+
This can be useful in various scenarios:
589
+
590
+
- Keeping a frequently used heavy screen in memory to avoid unmounting and remounting it for better performance when navigating back and forth.
591
+
- Keeping a screen with a video or audio player rendered to enable functionality such as background playback or picture-in-picture mode when the user navigates away from the screen.
592
+
593
+
If a route was removed from history while being retained, `retain(false)` will remove it from the navigation state and unmount the screen. If the route is still present in history, `retain(false)` will just unmark it, and the route will be removed from the navigation state when it's removed from history.
594
+
595
+
By default, the action applies to the route that dispatched it. If you want to retain a particular route, you can add a `source` property referring to the route key and `target` property referring to the navigation state key:
596
+
597
+
```js
598
+
import { StackActions } from '@react-navigation/native';
These changes also enable the new [retain](#stack-and-native-stack-navigators-now-support-retaining-screens) feature for Stack and Native Stack Navigators.
337
+
338
+
See [Navigation state](navigation-state.md) for more details.
339
+
336
340
##### Preloaded screens behave closer to regular screens
337
341
338
342
Previously, when a screen was preloaded in Stack and Native Stack Navigators, there were a few restrictions:
@@ -1048,6 +1052,29 @@ There is now a new `NavigationProvider` component that consolidates them, and ad
1048
1052
1049
1053
This is only necessary if you have custom components rendered in a navigator, e.g. buttons in a custom tab bar. Components rendered as part of a screen will have access to the `navigation` and `route` objects without any additional setup.
1050
1054
1055
+
### Stack and Native Stack Navigators now support retaining screens
1056
+
1057
+
Stack and Native Stack Navigators now support retaining screens after they are removed from the navigation history.
1058
+
1059
+
Retaining a screen keeps it in the navigation state and rendered in the background, so local component state is preserved. The screen can later be brought back into focus with `navigate`:
1060
+
1061
+
```js
1062
+
navigation.retain(true);
1063
+
```
1064
+
1065
+
To stop retaining the current screen, call `retain(false)`:
1066
+
1067
+
```js
1068
+
navigation.retain(false);
1069
+
```
1070
+
1071
+
This can be useful in various scenarios:
1072
+
1073
+
- Keeping a frequently used heavy screen in memory to avoid unmounting and remounting it for better performance when navigating back and forth.
1074
+
- Keeping a screen with a video or audio player rendered to enable functionality such as background playback or picture-in-picture mode when the user navigates away from the screen.
1075
+
1076
+
See [`retain`](stack-actions.md#retain) for more details.
1077
+
1051
1078
### `Header` from `@react-navigation/elements` has been reworked
1052
1079
1053
1080
The `Header` component from `@react-navigation/elements` has been reworked with various improvements:
0 commit comments