Skip to content

Commit 577ee38

Browse files
brentvatnesourcecode911
authored andcommitted
Add lifecycle polyfill and change away from componentWillMount where possible without too much refactoring
1 parent acf84e5 commit 577ee38

File tree

5 files changed

+57
-60
lines changed

5 files changed

+57
-60
lines changed

src/createNavigationContainer.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
import React from 'react';
22
import { Linking } from 'react-native';
3+
import withLifecyclePolyfill from 'react-lifecycles-compat';
4+
35
import { BackHandler } from './PlatformHelpers';
46
import NavigationActions from './NavigationActions';
57
import addNavigationHelpers from './addNavigationHelpers';
68
import invariant from './utils/invariant';
79

10+
function isStateful(props) {
11+
return !props.navigation;
12+
}
13+
14+
function validateProps(props) {
15+
if (isStateful(props)) {
16+
return;
17+
}
18+
19+
const { navigation, screenProps, ...containerProps } = props;
20+
21+
const keys = Object.keys(containerProps);
22+
23+
if (keys.length !== 0) {
24+
throw new Error(
25+
'This navigator has both navigation and container props, so it is ' +
26+
`unclear if it should own its own state. Remove props: "${keys.join(
27+
', '
28+
)}" ` +
29+
'if the navigator should get its state from the navigation prop. If the ' +
30+
'navigator should maintain its own state, do not pass a navigation prop.'
31+
);
32+
}
33+
}
34+
835
/**
936
* Create an HOC that injects the navigation and manages the navigation state
1037
* in case it's not passed from above.
@@ -18,12 +45,17 @@ export default function createNavigationContainer(Component) {
1845
static router = Component.router;
1946
static navigationOptions = null;
2047

48+
static getDerivedStateFromProps(nextProps, prevState) {
49+
validateProps(nextProps);
50+
return null;
51+
}
52+
2153
_actionEventSubscribers = new Set();
2254

2355
constructor(props) {
2456
super(props);
2557

26-
this._validateProps(props);
58+
validateProps(props);
2759

2860
this._initialAction = NavigationActions.init();
2961

@@ -48,28 +80,7 @@ export default function createNavigationContainer(Component) {
4880
}
4981

5082
_isStateful() {
51-
return !this.props.navigation;
52-
}
53-
54-
_validateProps(props) {
55-
if (this._isStateful()) {
56-
return;
57-
}
58-
59-
const { navigation, screenProps, ...containerProps } = props;
60-
61-
const keys = Object.keys(containerProps);
62-
63-
if (keys.length !== 0) {
64-
throw new Error(
65-
'This navigator has both navigation and container props, so it is ' +
66-
`unclear if it should own its own state. Remove props: "${keys.join(
67-
', '
68-
)}" ` +
69-
'if the navigator should get its state from the navigation prop. If the ' +
70-
'navigator should maintain its own state, do not pass a navigation prop.'
71-
);
72-
}
83+
return isStateful(this.props);
7384
}
7485

7586
_urlToPathAndParams(url) {
@@ -127,10 +138,6 @@ export default function createNavigationContainer(Component) {
127138
}
128139
}
129140

130-
componentWillReceiveProps(nextProps) {
131-
this._validateProps(nextProps);
132-
}
133-
134141
componentDidUpdate() {
135142
// Clear cached _nav every tick
136143
if (this._nav === this.state.nav) {
@@ -227,5 +234,5 @@ export default function createNavigationContainer(Component) {
227234
}
228235
}
229236

230-
return NavigationContainer;
237+
return withLifecyclePolyfill(NavigationContainer);
231238
}

src/views/Drawer/DrawerView.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ export default class DrawerView extends React.PureComponent {
1717
: this.props.navigationConfig.drawerWidth,
1818
};
1919

20-
componentWillMount() {
20+
componentDidMount() {
2121
Dimensions.addEventListener('change', this._updateWidth);
2222
}
2323

2424
componentWillUnmount() {
2525
Dimensions.removeEventListener('change', this._updateWidth);
2626
}
2727

28-
componentWillReceiveProps(nextProps) {
29-
const { isDrawerOpen } = nextProps.navigation.state;
30-
const wasDrawerOpen = this.props.navigation.state.isDrawerOpen;
28+
componentDidUpdate(prevProps, prevState) {
29+
const { isDrawerOpen } = this.props.navigation.state;
30+
const wasDrawerOpen = prevProps.navigation.state.isDrawerOpen;
31+
3132
if (isDrawerOpen && !wasDrawerOpen) {
3233
this._drawer.openDrawer();
3334
} else if (wasDrawerOpen && !isDrawerOpen) {

src/views/ResourceSavingSceneView.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import SceneView from './SceneView';
88
const FAR_FAR_AWAY = 3000; // this should be big enough to move the whole view out of its container
99

1010
class ResourceSavingSceneView extends React.PureComponent {
11+
static getDerivedStateFromProps(nextProps, prevState) {
12+
if (nextProps.isFocused && !prevState.awake) {
13+
return { awake: true };
14+
} else {
15+
return null;
16+
}
17+
}
18+
1119
constructor(props) {
1220
super();
1321

@@ -16,14 +24,6 @@ class ResourceSavingSceneView extends React.PureComponent {
1624
};
1725
}
1826

19-
static getDerivedStateFromProps(nextProps, prevState) {
20-
if (nextProps.isFocused && !prevState.awake) {
21-
return { awake: true };
22-
}
23-
24-
return null;
25-
}
26-
2727
render() {
2828
const { awake } = this.state;
2929
const {

src/views/StackView/StackViewLayout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ class StackViewLayout extends React.Component {
352352
return (
353353
<View {...handlers} style={containerStyle}>
354354
<View style={styles.scenes}>
355-
{scenes.map((s: *) => this._renderCard(s))}
355+
{scenes.map(s => this._renderCard(s))}
356356
</View>
357357
{floatingHeader}
358358
</View>

src/views/StackView/createPointerEventsContainer.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,14 @@ export default function createPointerEventsContainer(Component) {
1616
this._pointerEvents = this._computePointerEvents();
1717
}
1818

19-
componentWillMount() {
20-
this._onPositionChange = this._onPositionChange.bind(this);
21-
this._onComponentRef = this._onComponentRef.bind(this);
22-
}
23-
24-
componentDidMount() {
25-
this._bindPosition(this.props);
26-
}
27-
2819
componentWillUnmount() {
2920
this._positionListener && this._positionListener.remove();
3021
}
3122

32-
componentWillReceiveProps(nextProps) {
33-
this._bindPosition(nextProps);
34-
}
35-
3623
render() {
24+
this._bindPosition();
3725
this._pointerEvents = this._computePointerEvents();
26+
3827
return (
3928
<Component
4029
{...this.props}
@@ -44,33 +33,33 @@ export default function createPointerEventsContainer(Component) {
4433
);
4534
}
4635

47-
_onComponentRef(component) {
36+
_onComponentRef = component => {
4837
this._component = component;
4938
if (component) {
5039
invariant(
5140
typeof component.setNativeProps === 'function',
5241
'component must implement method `setNativeProps`'
5342
);
5443
}
55-
}
44+
};
5645

57-
_bindPosition(props) {
46+
_bindPosition() {
5847
this._positionListener && this._positionListener.remove();
5948
this._positionListener = new AnimatedValueSubscription(
60-
props.position,
49+
this.props.position,
6150
this._onPositionChange
6251
);
6352
}
6453

65-
_onPositionChange() {
54+
_onPositionChange = () => {
6655
if (this._component) {
6756
const pointerEvents = this._computePointerEvents();
6857
if (this._pointerEvents !== pointerEvents) {
6958
this._pointerEvents = pointerEvents;
7059
this._component.setNativeProps({ pointerEvents });
7160
}
7261
}
73-
}
62+
};
7463

7564
_computePointerEvents() {
7665
const { navigation, position, scene } = this.props;

0 commit comments

Comments
 (0)