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
Copy file name to clipboardExpand all lines: versioned_docs/version-8.x/native-stack-navigator.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -102,12 +102,15 @@ This controls what should happen when screens become inactive.
102
102
It supports the following values:
103
103
104
104
-`pause`: Effects are cleaned up - e.g. timers are cleared, subscriptions are removed, etc. This avoids unnecessary renders when the screen is inactive.
105
+
-`unmount`: The screen is unmounted when it becomes inactive.
105
106
-`none`: Screen renders normally.
106
107
107
108
Defaults to `pause`.
108
109
109
110
If you [`preload`](navigation-actions.md#preload) a screen, it won't be paused until after the first time it becomes focused.
110
111
112
+
If a screen contains a nested navigator, it won't be unmounted, but paused instead.
113
+
111
114
See [Inactive screens](navigation-lifecycle.md#inactive-screens) for more details.
Copy file name to clipboardExpand all lines: versioned_docs/version-8.x/navigation-lifecycle.md
+36-1Lines changed: 36 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -333,7 +333,7 @@ To know the focus state inside of an event listener, we can use the [`navigation
333
333
334
334
## Inactive screens
335
335
336
-
Many navigators also have an `inactiveBehavior` option that lets you "pause" screens when they are inactive:
336
+
Many navigators also have an `inactiveBehavior` option that lets you "pause" or "unmount" screens when they are inactive:
337
337
338
338
```js static2dynamic
339
339
constMyTabs=createBottomTabNavigator({
@@ -359,6 +359,8 @@ Here, "inactive" and "unfocused" have different meanings:
359
359
-[Preloaded](navigation-actions.md#preload) screens don't become inactive until after the first time they become focused, so their effects can run to initialize the screen
360
360
- Focus and blur are part of navigation lifecycle, but "inactive" is an optimization mechanism
361
361
362
+
### Paused screens
363
+
362
364
Paused screens internally use [`<Activity mode="hidden">`](https://react.dev/reference/react/Activity). When a screen is paused, the following things happen:
363
365
364
366
- Effects are cleaned up (similar to when a component unmounts)
When `inactiveBehavior` is set to `unmount`, screens will be unmounted when they become inactive. This means their content will be unmounted and their local state will be lost.
407
+
408
+
This is primarily useful for stack navigators where the list of screens can grow. Unmounting inactive screens can help free up resources.
409
+
410
+
To unmount inactive screens, you can set `inactiveBehavior` to `unmount`:
411
+
412
+
```js static2dynamic
413
+
constMyStack=createNativeStackNavigator({
414
+
screenOptions: {
415
+
// highlight-next-line
416
+
inactiveBehavior:'unmount',
417
+
},
418
+
screens: {
419
+
Home:createNativeStackScreen({
420
+
screen: HomeScreen,
421
+
}),
422
+
Details:createNativeStackScreen({
423
+
screen: DetailsScreen,
424
+
}),
425
+
},
426
+
});
427
+
```
428
+
429
+
Also consider reworking your navigation flow to avoid growing the stack indefinitely if possible - e.g. by using `pop: true` option in [`navigate`](navigation-actions.md#navigate) or using [`replace`](stack-actions.md#replace) when relevant.
430
+
431
+
:::info
432
+
433
+
Screens containing nested navigators won't be unmounted even when `inactiveBehavior` is set to `unmount`, and will be paused instead. This avoids losing nested navigation state.
434
+
435
+
:::
436
+
402
437
## Summary
403
438
404
439
- Screens stay mounted when navigating away from them
Copy file name to clipboardExpand all lines: versioned_docs/version-8.x/stack-navigator.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -121,12 +121,15 @@ This controls what should happen when screens become inactive.
121
121
It supports the following values:
122
122
123
123
-`pause`: Effects are cleaned up - e.g. timers are cleared, subscriptions are removed, etc. This avoids unnecessary renders when the screen is inactive.
124
+
-`unmount`: The screen is unmounted when it becomes inactive.
124
125
-`none`: Screen renders normally.
125
126
126
127
Defaults to `pause`.
127
128
128
129
If you [`preload`](navigation-actions.md#preload) a screen, it won't be paused until after the first time it becomes focused.
129
130
131
+
If a screen contains a nested navigator, it won't be unmounted, but paused instead.
132
+
130
133
See [Inactive screens](navigation-lifecycle.md#inactive-screens) for more details.
0 commit comments