Skip to content

Commit 9137f7f

Browse files
elyobotimdorr
authored andcommitted
Use UNSAFE_ lifecycle event names where supported. (#1383)
This provides forward compatibility with React 17.x for those still using the react-redux 5.x series by detecting support for the UNSAFE_ prefixed lifecycle event names (using `React.createContext` as a feature check as it avoids semver checking but was introduced in React 16.3 at the same time as the UNSAFE_ prefixes).
1 parent 5cdb83a commit 9137f7f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/components/Provider.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { Component, Children } from 'react'
1+
import React, { Component, Children } from 'react'
22
import PropTypes from 'prop-types'
33
import { storeShape, subscriptionShape } from '../utils/PropTypes'
44
import warning from '../utils/warning'
55

6+
const prefixUnsafeLifecycleMethods = parseFloat(React.version) >= 16.3
7+
68
let didWarnAboutReceivingStore = false
79
function warnAboutReceivingStore() {
810
if (didWarnAboutReceivingStore) {
@@ -38,7 +40,11 @@ export function createProvider(storeKey = 'store') {
3840
}
3941

4042
if (process.env.NODE_ENV !== 'production') {
41-
Provider.prototype.componentWillReceiveProps = function (nextProps) {
43+
// Use UNSAFE_ event name where supported
44+
const eventName = prefixUnsafeLifecycleMethods
45+
? 'UNSAFE_componentWillReceiveProps'
46+
: 'componentWillReceiveProps'
47+
Provider.prototype[eventName] = function (nextProps) {
4248
if (this[storeKey] !== nextProps.store) {
4349
warnAboutReceivingStore()
4450
}

src/components/connectAdvanced.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import hoistStatics from 'hoist-non-react-statics'
22
import invariant from 'invariant'
3-
import { Component, createElement } from 'react'
3+
import React, { Component, createElement } from 'react'
44
import { isValidElementType } from 'react-is'
55

66
import Subscription from '../utils/Subscription'
77
import { storeShape, subscriptionShape } from '../utils/PropTypes'
88

9+
const prefixUnsafeLifecycleMethods = parseFloat(React.version) >= 16.3
10+
911
let hotReloadingVersion = 0
1012
const dummyState = {}
1113
function noop() {}
@@ -160,6 +162,7 @@ export default function connectAdvanced(
160162
if (this.selector.shouldComponentUpdate) this.forceUpdate()
161163
}
162164

165+
// Note: this is renamed below to the UNSAFE_ version in React >=16.3.0
163166
componentWillReceiveProps(nextProps) {
164167
this.selector.run(nextProps)
165168
}
@@ -262,6 +265,12 @@ export default function connectAdvanced(
262265
}
263266
}
264267

268+
if (prefixUnsafeLifecycleMethods) {
269+
// Use UNSAFE_ event name where supported
270+
Connect.UNSAFE_componentWillReceiveProps = Connect.componentWillReceiveProps
271+
delete Connect.componentWillReceiveProps
272+
}
273+
265274
/* eslint-enable react/no-deprecated */
266275

267276
Connect.WrappedComponent = WrappedComponent

0 commit comments

Comments
 (0)