Skip to content

Commit

Permalink
Use UNSAFE_ lifecycle event names where supported. (#1383)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
elyobo authored and timdorr committed Aug 28, 2019
1 parent 5cdb83a commit 9137f7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/components/Provider.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Component, Children } from 'react'
import React, { Component, Children } from 'react'
import PropTypes from 'prop-types'
import { storeShape, subscriptionShape } from '../utils/PropTypes'
import warning from '../utils/warning'

const prefixUnsafeLifecycleMethods = parseFloat(React.version) >= 16.3

let didWarnAboutReceivingStore = false
function warnAboutReceivingStore() {
if (didWarnAboutReceivingStore) {
Expand Down Expand Up @@ -38,7 +40,11 @@ export function createProvider(storeKey = 'store') {
}

if (process.env.NODE_ENV !== 'production') {
Provider.prototype.componentWillReceiveProps = function (nextProps) {
// Use UNSAFE_ event name where supported
const eventName = prefixUnsafeLifecycleMethods
? 'UNSAFE_componentWillReceiveProps'
: 'componentWillReceiveProps'
Provider.prototype[eventName] = function (nextProps) {
if (this[storeKey] !== nextProps.store) {
warnAboutReceivingStore()
}
Expand Down
11 changes: 10 additions & 1 deletion src/components/connectAdvanced.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import hoistStatics from 'hoist-non-react-statics'
import invariant from 'invariant'
import { Component, createElement } from 'react'
import React, { Component, createElement } from 'react'
import { isValidElementType } from 'react-is'

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

const prefixUnsafeLifecycleMethods = parseFloat(React.version) >= 16.3

let hotReloadingVersion = 0
const dummyState = {}
function noop() {}
Expand Down Expand Up @@ -160,6 +162,7 @@ export default function connectAdvanced(
if (this.selector.shouldComponentUpdate) this.forceUpdate()
}

// Note: this is renamed below to the UNSAFE_ version in React >=16.3.0
componentWillReceiveProps(nextProps) {
this.selector.run(nextProps)
}
Expand Down Expand Up @@ -262,6 +265,12 @@ export default function connectAdvanced(
}
}

if (prefixUnsafeLifecycleMethods) {
// Use UNSAFE_ event name where supported
Connect.UNSAFE_componentWillReceiveProps = Connect.componentWillReceiveProps
delete Connect.componentWillReceiveProps
}

/* eslint-enable react/no-deprecated */

Connect.WrappedComponent = WrappedComponent
Expand Down

0 comments on commit 9137f7f

Please sign in to comment.