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

Do not fire getDerivedStateFromProps unless props or state have changed #12802

Merged
merged 1 commit into from May 14, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 20 additions & 21 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Expand Up @@ -802,16 +802,6 @@ export default function(
);
newState = workInProgress.memoizedState;
}

if (typeof getDerivedStateFromProps === 'function') {
applyDerivedStateFromProps(
workInProgress,
getDerivedStateFromProps,
newProps,
);
newState = workInProgress.memoizedState;
}

if (
oldProps === newProps &&
oldState === newState &&
Expand All @@ -829,6 +819,15 @@ export default function(
return false;
}

if (typeof getDerivedStateFromProps === 'function') {
applyDerivedStateFromProps(
workInProgress,
getDerivedStateFromProps,
newProps,
);
newState = workInProgress.memoizedState;
}

const shouldUpdate = checkShouldComponentUpdate(
workInProgress,
oldProps,
Expand Down Expand Up @@ -937,17 +936,6 @@ export default function(
newState = workInProgress.memoizedState;
}

if (typeof getDerivedStateFromProps === 'function') {
if (fireGetDerivedStateFromPropsOnStateUpdates || oldProps !== newProps) {
applyDerivedStateFromProps(
workInProgress,
getDerivedStateFromProps,
newProps,
);
newState = workInProgress.memoizedState;
}
}

if (
oldProps === newProps &&
oldState === newState &&
Expand Down Expand Up @@ -978,6 +966,17 @@ export default function(
return false;
}

if (typeof getDerivedStateFromProps === 'function') {
if (fireGetDerivedStateFromPropsOnStateUpdates || oldProps !== newProps) {
applyDerivedStateFromProps(
workInProgress,
getDerivedStateFromProps,
newProps,
);
newState = workInProgress.memoizedState;
}
}

const shouldUpdate = checkShouldComponentUpdate(
workInProgress,
oldProps,
Expand Down
Expand Up @@ -1464,6 +1464,39 @@ describe('ReactIncremental', () => {
expect(instance.state).toEqual({foo: 'foo'});
});

it('does not call getDerivedStateFromProps if neither state nor props have changed', () => {
class Parent extends React.Component {
state = {parentRenders: 0};
static getDerivedStateFromProps(props, prevState) {
ReactNoop.yield('getDerivedStateFromProps');
return prevState.parentRenders + 1;
}
render() {
ReactNoop.yield('Parent');
return <Child parentRenders={this.state.parentRenders} ref={child} />;
}
}

class Child extends React.Component {
render() {
ReactNoop.yield('Child');
return this.props.parentRenders;
}
}

const child = React.createRef(null);
ReactNoop.render(<Parent />);
expect(ReactNoop.flush()).toEqual([
'getDerivedStateFromProps',
'Parent',
'Child',
]);

// Schedule an update on the child. The parent should not re-render.
child.current.setState({});
expect(ReactNoop.flush()).toEqual(['Child']);
});

it('does not call getDerivedStateFromProps for state-only updates if feature flag is disabled', () => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
Expand Down