Skip to content

Commit cd665bf

Browse files
committed
Document inactiveBehavior: 'unmount'
1 parent bace418 commit cd665bf

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

versioned_docs/version-8.x/native-stack-navigator.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ This controls what should happen when screens become inactive.
102102
It supports the following values:
103103

104104
- `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.
105106
- `none`: Screen renders normally.
106107

107108
Defaults to `pause`.
108109

109110
If you [`preload`](navigation-actions.md#preload) a screen, it won't be paused until after the first time it becomes focused.
110111

112+
If a screen contains a nested navigator, it won't be unmounted, but paused instead.
113+
111114
See [Inactive screens](navigation-lifecycle.md#inactive-screens) for more details.
112115

113116
#### `title`

versioned_docs/version-8.x/navigation-lifecycle.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ To know the focus state inside of an event listener, we can use the [`navigation
333333

334334
## Inactive screens
335335

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:
337337

338338
```js static2dynamic
339339
const MyTabs = createBottomTabNavigator({
@@ -359,6 +359,8 @@ Here, "inactive" and "unfocused" have different meanings:
359359
- [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
360360
- Focus and blur are part of navigation lifecycle, but "inactive" is an optimization mechanism
361361

362+
### Paused screens
363+
362364
Paused screens internally use [`<Activity mode="hidden">`](https://react.dev/reference/react/Activity). When a screen is paused, the following things happen:
363365

364366
- Effects are cleaned up (similar to when a component unmounts)
@@ -399,6 +401,39 @@ const MyTabs = createBottomTabNavigator({
399401
});
400402
```
401403

404+
### Unmounted screens
405+
406+
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+
const MyStack = 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+
402437
## Summary
403438

404439
- Screens stay mounted when navigating away from them

versioned_docs/version-8.x/stack-navigator.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,15 @@ This controls what should happen when screens become inactive.
121121
It supports the following values:
122122

123123
- `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.
124125
- `none`: Screen renders normally.
125126

126127
Defaults to `pause`.
127128

128129
If you [`preload`](navigation-actions.md#preload) a screen, it won't be paused until after the first time it becomes focused.
129130

131+
If a screen contains a nested navigator, it won't be unmounted, but paused instead.
132+
130133
See [Inactive screens](navigation-lifecycle.md#inactive-screens) for more details.
131134

132135
#### `title`

0 commit comments

Comments
 (0)