Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UNSAFE_ lifecycle event names where supported. #1383

Merged
merged 1 commit into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elyobo @timdorr Thanks for fixing this, but there's still one more issue here. parseFloat("16.10.1") parses as 16.1 which is of course less than 16.3, so this fails on the latest release of React.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's not good... Can you work up a fix PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I got it: #1407

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, we've gone back to a feature check like this PR originally shipped with before #1383 (comment) :D Thanks for the pick up Sam.


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