Skip to content

Commit eb59ccf

Browse files
brentvatnesourcecode911
authored andcommitted
Swap addListener out for isFocused prop on ResourceSavingSceneView (react-navigation#3700)
1 parent 03fd95c commit eb59ccf

File tree

4 files changed

+44
-56
lines changed

4 files changed

+44
-56
lines changed

package.json

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"url": "git@github.com:react-navigation/react-navigation.git",
88
"type": "git"
99
},
10-
"author":
11-
"Adam Miskiewicz <adam@sk3vy.com>, Eric Vicenti <ericvicenti@gmail.com>",
10+
"author": "Adam Miskiewicz <adam@sk3vy.com>, Eric Vicenti <ericvicenti@gmail.com>",
1211
"license": "BSD-2-Clause",
1312
"scripts": {
1413
"start": "npm run ios",
@@ -22,7 +21,9 @@
2221
"format": "eslint --fix .",
2322
"precommit": "lint-staged"
2423
},
25-
"files": ["src"],
24+
"files": [
25+
"src"
26+
],
2627
"peerDependencies": {
2728
"react": "*",
2829
"react-native": "*"
@@ -32,6 +33,7 @@
3233
"hoist-non-react-statics": "^2.2.0",
3334
"path-to-regexp": "^1.7.0",
3435
"prop-types": "^15.5.10",
36+
"react-lifecycles-compat": "^1.0.2",
3537
"react-native-drawer-layout-polyfill": "^1.3.2",
3638
"react-native-safe-area-view": "^0.7.0",
3739
"react-native-tab-view": "^0.0.74"
@@ -105,13 +107,23 @@
105107
"notify": true,
106108
"preset": "react-native",
107109
"testRegex": "./src/.*\\-test\\.js$",
108-
"setupFiles": ["<rootDir>/jest-setup.js"],
110+
"setupFiles": [
111+
"<rootDir>/jest-setup.js"
112+
],
109113
"coverageDirectory": "./coverage/",
110114
"collectCoverage": true,
111-
"coverageReporters": ["lcov"],
112-
"collectCoverageFrom": ["src/**/*.js"],
113-
"coveragePathIgnorePatterns": ["jest-setup.js"],
114-
"modulePathIgnorePatterns": ["examples"]
115+
"coverageReporters": [
116+
"lcov"
117+
],
118+
"collectCoverageFrom": [
119+
"src/**/*.js"
120+
],
121+
"coveragePathIgnorePatterns": [
122+
"jest-setup.js"
123+
],
124+
"modulePathIgnorePatterns": [
125+
"examples"
126+
]
115127
},
116128
"lint-staged": {
117129
"*.js": [

src/views/ResourceSavingSceneView.js

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,33 @@
11
import React from 'react';
22
import { Platform, StyleSheet, View } from 'react-native';
33
import PropTypes from 'prop-types';
4+
import withLifecyclePolyfill from 'react-lifecycles-compat';
45

56
import SceneView from './SceneView';
67

78
const FAR_FAR_AWAY = 3000; // this should be big enough to move the whole view out of its container
89

9-
export default class ResourceSavingSceneView extends React.PureComponent {
10+
class ResourceSavingSceneView extends React.PureComponent {
1011
constructor(props) {
1112
super();
1213

13-
const key = props.childNavigation.state.key;
14-
const focusedIndex = props.navigation.state.index;
15-
const focusedKey = props.navigation.state.routes[focusedIndex].key;
16-
const isFocused = key === focusedKey;
17-
1814
this.state = {
19-
awake: props.lazy ? isFocused : true,
20-
visible: isFocused,
15+
awake: props.lazy ? props.isFocused : true,
2116
};
2217
}
2318

24-
componentWillMount() {
25-
this._actionListener = this.props.navigation.addListener(
26-
'action',
27-
this._onAction
28-
);
29-
}
19+
static getDerivedStateFromProps(nextProps, prevState) {
20+
if (nextProps.isFocused && !prevState.awake) {
21+
return { awake: true };
22+
}
3023

31-
componentWillUnmount() {
32-
this._actionListener.remove();
24+
return null;
3325
}
3426

3527
render() {
36-
const { awake, visible } = this.state;
28+
const { awake } = this.state;
3729
const {
30+
isFocused,
3831
childNavigation,
3932
navigation,
4033
removeClippedSubviews,
@@ -49,12 +42,12 @@ export default class ResourceSavingSceneView extends React.PureComponent {
4942
removeClippedSubviews={
5043
Platform.OS === 'android'
5144
? removeClippedSubviews
52-
: !visible && removeClippedSubviews
45+
: !isFocused && removeClippedSubviews
5346
}
5447
>
5548
<View
5649
style={
57-
this._mustAlwaysBeVisible() || visible
50+
this._mustAlwaysBeVisible() || isFocused
5851
? styles.innerAttached
5952
: styles.innerDetached
6053
}
@@ -68,33 +61,6 @@ export default class ResourceSavingSceneView extends React.PureComponent {
6861
_mustAlwaysBeVisible = () => {
6962
return this.props.animationEnabled || this.props.swipeEnabled;
7063
};
71-
72-
_onAction = payload => {
73-
// We do not care about transition complete events, they won't actually change the state
74-
if (
75-
payload.action.type == 'Navigation/COMPLETE_TRANSITION' ||
76-
!payload.state
77-
) {
78-
return;
79-
}
80-
81-
const { routes, index } = payload.state;
82-
const key = this.props.childNavigation.state.key;
83-
84-
if (routes[index].key === key) {
85-
if (!this.state.visible) {
86-
let nextState = { visible: true };
87-
if (!this.state.awake) {
88-
nextState.awake = true;
89-
}
90-
this.setState(nextState);
91-
}
92-
} else {
93-
if (this.state.visible) {
94-
this.setState({ visible: false });
95-
}
96-
}
97-
};
9864
}
9965

10066
const styles = StyleSheet.create({
@@ -110,3 +76,5 @@ const styles = StyleSheet.create({
11076
top: FAR_FAR_AWAY,
11177
},
11278
});
79+
80+
export default withLifecyclePolyfill(ResourceSavingSceneView);

src/views/TabView/TabView.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,28 @@ class TabView extends React.PureComponent {
2121
};
2222

2323
_renderScene = ({ route }) => {
24-
const { screenProps, descriptors } = this.props;
24+
const { screenProps, navigation, descriptors } = this.props;
2525
const {
2626
lazy,
2727
removeClippedSubviews,
2828
animationEnabled,
2929
swipeEnabled,
3030
} = this.props.navigationConfig;
3131
const descriptor = descriptors[route.key];
32+
const focusedIndex = navigation.state.index;
33+
const focusedKey = navigation.state.routes[focusedIndex].key;
34+
const key = route.key;
3235
const TabComponent = descriptor.getComponent();
3336
return (
3437
<ResourceSavingSceneView
3538
lazy={lazy}
39+
isFocused={focusedKey === key}
3640
removeClippedSubViews={removeClippedSubviews}
3741
animationEnabled={animationEnabled}
3842
swipeEnabled={swipeEnabled}
3943
screenProps={screenProps}
4044
component={TabComponent}
41-
navigation={this.props.navigation}
45+
navigation={navigation}
4246
childNavigation={descriptor.navigation}
4347
/>
4448
);

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4424,6 +4424,10 @@ react-devtools-core@3.0.0:
44244424
shell-quote "^1.6.1"
44254425
ws "^2.0.3"
44264426

4427+
react-lifecycles-compat@^1.0.2:
4428+
version "1.0.2"
4429+
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-1.0.2.tgz#551d8b1d156346e5fcf30ffac9b32ce3f78b8850"
4430+
44274431
react-native-dismiss-keyboard@1.0.0:
44284432
version "1.0.0"
44294433
resolved "https://registry.yarnpkg.com/react-native-dismiss-keyboard/-/react-native-dismiss-keyboard-1.0.0.tgz#32886242b3f2317e121f3aeb9b0a585e2b879b49"

0 commit comments

Comments
 (0)