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

Fix getDerivedStateFromProps usage #980

Closed
wants to merge 20 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/components/connectAdvanced.js
@@ -1,6 +1,6 @@
import hoistStatics from 'hoist-non-react-statics'
import invariant from 'invariant'
import { Component, createElement } from 'react'
import React, { Component, createElement } from 'react'
import { polyfill } from 'react-lifecycles-compat'
import shallowEqual from '../utils/shallowEqual'

Expand All @@ -10,6 +10,12 @@ import { storeShape, subscriptionShape } from '../utils/PropTypes'
let hotReloadingVersion = 0
function noop() {}

function isOldReact() {
const version = React.version.split('.')
if (+version[0] < 16) return true
if (+version[0] > 16) return false
return +version[1] < 4
}
export default function connectAdvanced(
/*
selectorFactory is a func that is responsible for returning the selector function used to
Expand Down Expand Up @@ -140,7 +146,11 @@ export default function connectAdvanced(

static getDerivedStateFromProps(props, state) {
let ret = null
if (state.lastNotify !== state.notifyNestedSubs) {
// this next check only should trigger if gDSFP reacts to state changes
// in React 16.3, it doesn't. react-lifecycles-compat matches React 16.3 behavior
// if a future version of react-lifecycles-compat DOES trigger on state changes
// the isOldReact() call must be removed
if (!isOldReact() && state.lastNotify !== state.notifyNestedSubs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

No reason to do the string splitting every time this runs. We can just do it once when connect() is called, or even once at the top of the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

duh. fixing :)

ret = { lastNotify: state.notifyNestedSubs }
if (shallowEqual(props, state.props) || state.error) {
return ret
Expand Down