Skip to content

Commit 0b4bf1d

Browse files
authored
refactor: drop unmountInactiveScreens in favor of unmountOnBlur… (react-navigation#317)
The `unmountInactiveScreens` prop lets user unmount all inactive screens for the whole navigator when they go out of focus. It'll be better to have the option to do that per screen, so I have added the `unmountOnBlur` option instead. To get the previous behaviour, user can specify the option in `screenOptions`.
1 parent 5a3f835 commit 0b4bf1d

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

packages/bottom-tabs/src/types.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ export type BottomTabNavigationOptions = {
9999
* Renders `TouchableWithoutFeedback` by default.
100100
*/
101101
tabBarButton?: (props: BottomTabBarButtonProps) => React.ReactNode;
102+
103+
/**
104+
* Whether this screen should be unmounted when navigating away from it.
105+
* Defaults to `false`.
106+
*/
107+
unmountOnBlur?: boolean;
102108
};
103109

104110
export type BottomTabDescriptor = Descriptor<
@@ -118,11 +124,6 @@ export type BottomTabNavigationConfig = {
118124
* Set it to `false` if you want to render all screens on initial render.
119125
*/
120126
lazy?: boolean;
121-
/**
122-
* Whether a screen should be unmounted when navigating away from it.
123-
* Defaults to `false`.
124-
*/
125-
unmountInactiveScreens?: boolean;
126127
/**
127128
* Function that returns a React element to display as the tab bar.
128129
*/

packages/bottom-tabs/src/views/BottomTabView.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default class BottomTabView extends React.Component<Props, State> {
9292
};
9393

9494
render() {
95-
const { state, descriptors, lazy, unmountInactiveScreens } = this.props;
95+
const { state, descriptors, lazy } = this.props;
9696
const { routes } = state;
9797
const { loaded } = this.state;
9898

@@ -101,25 +101,27 @@ export default class BottomTabView extends React.Component<Props, State> {
101101
<View style={styles.container}>
102102
<ScreenContainer style={styles.pages}>
103103
{routes.map((route, index) => {
104-
if (unmountInactiveScreens && index !== state.index) {
104+
const descriptor = descriptors[route.key];
105+
const { unmountOnBlur } = descriptor.options;
106+
const isFocused = state.index === index;
107+
108+
if (unmountOnBlur && !isFocused) {
105109
return null;
106110
}
107111

108-
if (lazy && !loaded.includes(index)) {
112+
if (lazy && !loaded.includes(index) && !isFocused) {
109113
// Don't render a screen if we've never navigated to it
110114
return null;
111115
}
112116

113-
const isFocused = state.index === index;
114-
115117
return (
116118
<ResourceSavingScene
117119
key={route.key}
118120
style={StyleSheet.absoluteFill}
119121
isVisible={isFocused}
120122
>
121123
<SceneContent isFocused={isFocused}>
122-
{descriptors[route.key].render()}
124+
{descriptor.render()}
123125
</SceneContent>
124126
</ResourceSavingScene>
125127
);

packages/drawer/src/types.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ export type DrawerNavigationConfig<T = DrawerContentOptions> = {
6363
* Set it to `false` if you want to render all screens on initial render.
6464
*/
6565
lazy?: boolean;
66-
/**
67-
* Whether a screen should be unmounted when navigating away from it.
68-
* Defaults to `false`.
69-
*/
70-
unmountInactiveScreens?: boolean;
7166
/**
7267
* Function that returns React element to render as the content of the drawer, for example, navigation items.
7368
* Defaults to `DrawerContent`.
@@ -117,6 +112,11 @@ export type DrawerNavigationOptions = {
117112
* Defaults to `true`
118113
*/
119114
gestureEnabled?: boolean;
115+
/**
116+
* Whether this screen should be unmounted when navigating away from it.
117+
* Defaults to `false`.
118+
*/
119+
unmountOnBlur?: boolean;
120120
};
121121

122122
export type DrawerContentComponentProps<T = DrawerContentOptions> = T & {

packages/drawer/src/views/DrawerView.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export default function DrawerView({
7777
gestureHandlerProps,
7878
minSwipeDistance,
7979
sceneContainerStyle,
80-
unmountInactiveScreens,
8180
}: Props) {
8281
const [loaded, setLoaded] = React.useState([state.index]);
8382
const [drawerWidth, setDrawerWidth] = React.useState(() =>
@@ -135,18 +134,19 @@ export default function DrawerView({
135134
return (
136135
<ScreenContainer style={styles.content}>
137136
{state.routes.map((route, index) => {
138-
if (unmountInactiveScreens && index !== state.index) {
137+
const descriptor = descriptors[route.key];
138+
const { unmountOnBlur } = descriptor.options;
139+
const isFocused = state.index === index;
140+
141+
if (unmountOnBlur && !isFocused) {
139142
return null;
140143
}
141144

142-
if (lazy && !loaded.includes(index) && index !== state.index) {
145+
if (lazy && !loaded.includes(index) && !isFocused) {
143146
// Don't render a screen if we've never navigated to it
144147
return null;
145148
}
146149

147-
const isFocused = state.index === index;
148-
const descriptor = descriptors[route.key];
149-
150150
return (
151151
<ResourceSavingScene
152152
key={route.key}

0 commit comments

Comments
 (0)