Swap addListener out for isFocused prop on ResourceSavingSceneView (#3700)

This commit is contained in:
Brent Vatne
2018-03-09 10:55:00 -08:00
committed by GitHub
parent 47f357f332
commit 81a86fa091
4 changed files with 44 additions and 56 deletions

View File

@@ -7,8 +7,7 @@
"url": "git@github.com:react-navigation/react-navigation.git", "url": "git@github.com:react-navigation/react-navigation.git",
"type": "git" "type": "git"
}, },
"author": "author": "Adam Miskiewicz <adam@sk3vy.com>, Eric Vicenti <ericvicenti@gmail.com>",
"Adam Miskiewicz <adam@sk3vy.com>, Eric Vicenti <ericvicenti@gmail.com>",
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"scripts": { "scripts": {
"start": "npm run ios", "start": "npm run ios",
@@ -22,7 +21,9 @@
"format": "eslint --fix .", "format": "eslint --fix .",
"precommit": "lint-staged" "precommit": "lint-staged"
}, },
"files": ["src"], "files": [
"src"
],
"peerDependencies": { "peerDependencies": {
"react": "*", "react": "*",
"react-native": "*" "react-native": "*"
@@ -32,6 +33,7 @@
"hoist-non-react-statics": "^2.2.0", "hoist-non-react-statics": "^2.2.0",
"path-to-regexp": "^1.7.0", "path-to-regexp": "^1.7.0",
"prop-types": "^15.5.10", "prop-types": "^15.5.10",
"react-lifecycles-compat": "^1.0.2",
"react-native-drawer-layout-polyfill": "^1.3.2", "react-native-drawer-layout-polyfill": "^1.3.2",
"react-native-safe-area-view": "^0.7.0", "react-native-safe-area-view": "^0.7.0",
"react-native-tab-view": "^0.0.74" "react-native-tab-view": "^0.0.74"
@@ -63,13 +65,23 @@
"notify": true, "notify": true,
"preset": "react-native", "preset": "react-native",
"testRegex": "./src/.*\\-test\\.js$", "testRegex": "./src/.*\\-test\\.js$",
"setupFiles": ["<rootDir>/jest-setup.js"], "setupFiles": [
"<rootDir>/jest-setup.js"
],
"coverageDirectory": "./coverage/", "coverageDirectory": "./coverage/",
"collectCoverage": true, "collectCoverage": true,
"coverageReporters": ["lcov"], "coverageReporters": [
"collectCoverageFrom": ["src/**/*.js"], "lcov"
"coveragePathIgnorePatterns": ["jest-setup.js"], ],
"modulePathIgnorePatterns": ["examples"] "collectCoverageFrom": [
"src/**/*.js"
],
"coveragePathIgnorePatterns": [
"jest-setup.js"
],
"modulePathIgnorePatterns": [
"examples"
]
}, },
"lint-staged": { "lint-staged": {
"*.js": [ "*.js": [

View File

@@ -1,40 +1,33 @@
import React from 'react'; import React from 'react';
import { Platform, StyleSheet, View } from 'react-native'; import { Platform, StyleSheet, View } from 'react-native';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import withLifecyclePolyfill from 'react-lifecycles-compat';
import SceneView from './SceneView'; import SceneView from './SceneView';
const FAR_FAR_AWAY = 3000; // this should be big enough to move the whole view out of its container const FAR_FAR_AWAY = 3000; // this should be big enough to move the whole view out of its container
export default class ResourceSavingSceneView extends React.PureComponent { class ResourceSavingSceneView extends React.PureComponent {
constructor(props) { constructor(props) {
super(); super();
const key = props.childNavigation.state.key;
const focusedIndex = props.navigation.state.index;
const focusedKey = props.navigation.state.routes[focusedIndex].key;
const isFocused = key === focusedKey;
this.state = { this.state = {
awake: props.lazy ? isFocused : true, awake: props.lazy ? props.isFocused : true,
visible: isFocused,
}; };
} }
componentWillMount() { static getDerivedStateFromProps(nextProps, prevState) {
this._actionListener = this.props.navigation.addListener( if (nextProps.isFocused && !prevState.awake) {
'action', return { awake: true };
this._onAction }
);
}
componentWillUnmount() { return null;
this._actionListener.remove();
} }
render() { render() {
const { awake, visible } = this.state; const { awake } = this.state;
const { const {
isFocused,
childNavigation, childNavigation,
navigation, navigation,
removeClippedSubviews, removeClippedSubviews,
@@ -49,12 +42,12 @@ export default class ResourceSavingSceneView extends React.PureComponent {
removeClippedSubviews={ removeClippedSubviews={
Platform.OS === 'android' Platform.OS === 'android'
? removeClippedSubviews ? removeClippedSubviews
: !visible && removeClippedSubviews : !isFocused && removeClippedSubviews
} }
> >
<View <View
style={ style={
this._mustAlwaysBeVisible() || visible this._mustAlwaysBeVisible() || isFocused
? styles.innerAttached ? styles.innerAttached
: styles.innerDetached : styles.innerDetached
} }
@@ -68,33 +61,6 @@ export default class ResourceSavingSceneView extends React.PureComponent {
_mustAlwaysBeVisible = () => { _mustAlwaysBeVisible = () => {
return this.props.animationEnabled || this.props.swipeEnabled; return this.props.animationEnabled || this.props.swipeEnabled;
}; };
_onAction = payload => {
// We do not care about transition complete events, they won't actually change the state
if (
payload.action.type == 'Navigation/COMPLETE_TRANSITION' ||
!payload.state
) {
return;
}
const { routes, index } = payload.state;
const key = this.props.childNavigation.state.key;
if (routes[index].key === key) {
if (!this.state.visible) {
let nextState = { visible: true };
if (!this.state.awake) {
nextState.awake = true;
}
this.setState(nextState);
}
} else {
if (this.state.visible) {
this.setState({ visible: false });
}
}
};
} }
const styles = StyleSheet.create({ const styles = StyleSheet.create({
@@ -110,3 +76,5 @@ const styles = StyleSheet.create({
top: FAR_FAR_AWAY, top: FAR_FAR_AWAY,
}, },
}); });
export default withLifecyclePolyfill(ResourceSavingSceneView);

View File

@@ -21,7 +21,7 @@ class TabView extends React.PureComponent {
}; };
_renderScene = ({ route }) => { _renderScene = ({ route }) => {
const { screenProps, descriptors } = this.props; const { screenProps, navigation, descriptors } = this.props;
const { const {
lazy, lazy,
removeClippedSubviews, removeClippedSubviews,
@@ -29,16 +29,20 @@ class TabView extends React.PureComponent {
swipeEnabled, swipeEnabled,
} = this.props.navigationConfig; } = this.props.navigationConfig;
const descriptor = descriptors[route.key]; const descriptor = descriptors[route.key];
const focusedIndex = navigation.state.index;
const focusedKey = navigation.state.routes[focusedIndex].key;
const key = route.key;
const TabComponent = descriptor.getComponent(); const TabComponent = descriptor.getComponent();
return ( return (
<ResourceSavingSceneView <ResourceSavingSceneView
lazy={lazy} lazy={lazy}
isFocused={focusedKey === key}
removeClippedSubViews={removeClippedSubviews} removeClippedSubViews={removeClippedSubviews}
animationEnabled={animationEnabled} animationEnabled={animationEnabled}
swipeEnabled={swipeEnabled} swipeEnabled={swipeEnabled}
screenProps={screenProps} screenProps={screenProps}
component={TabComponent} component={TabComponent}
navigation={this.props.navigation} navigation={navigation}
childNavigation={descriptor.navigation} childNavigation={descriptor.navigation}
/> />
); );

View File

@@ -4424,6 +4424,10 @@ react-devtools-core@3.0.0:
shell-quote "^1.6.1" shell-quote "^1.6.1"
ws "^2.0.3" ws "^2.0.3"
react-lifecycles-compat@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-1.0.2.tgz#551d8b1d156346e5fcf30ffac9b32ce3f78b8850"
react-native-dismiss-keyboard@1.0.0: react-native-dismiss-keyboard@1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/react-native-dismiss-keyboard/-/react-native-dismiss-keyboard-1.0.0.tgz#32886242b3f2317e121f3aeb9b0a585e2b879b49" resolved "https://registry.yarnpkg.com/react-native-dismiss-keyboard/-/react-native-dismiss-keyboard-1.0.0.tgz#32886242b3f2317e121f3aeb9b0a585e2b879b49"