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

Specify that gDSFP is not called if neither props nor state have changed #1123

Merged
merged 1 commit into from
May 27, 2018
Merged
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
45 changes: 43 additions & 2 deletions test/browser/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ describe('Lifecycle methods', () => {
expect(Foo.getDerivedStateFromProps).to.have.been.called;
});

// TODO: Consider if componentWillUpdate should still be called
// Likely, componentWillUpdate should not be called only if getSnapshotBeforeUpdate is implemented
it('should NOT invoke deprecated lifecycles (cWM/cWRP) if new static gDSFP is present', () => {
class Foo extends Component {
static getDerivedStateFromProps() {}
Expand All @@ -214,6 +212,49 @@ describe('Lifecycle methods', () => {
expect(Foo.prototype.componentWillReceiveProps).to.not.have.been.called;
});

it('is not called if neither state nor props have changed', () => {
let logs = [];
let childRef;

class Parent extends Component {
constructor(props) {
super(props);
this.state = { parentRenders: 0 };
}

static getDerivedStateFromProps(props, prevState) {
logs.push('parent getDerivedStateFromProps');
return prevState.parentRenders + 1;
}

render() {
logs.push('parent render');
return <Child parentRenders={this.state.parentRenders} ref={child => childRef = child} />;
}
}

class Child extends Component {
render() {
logs.push('child render');
return this.props.parentRenders;
}
}

render(<Parent />, scratch);
expect(logs).to.deep.equal([
'parent getDerivedStateFromProps',
'parent render',
'child render'
]);

logs = [];
childRef.setState({});
rerender();
expect(logs).to.deep.equal([
'child render'
]);
});

// TODO: Investigate this test:
// [should not override state with stale values if prevState is spread within getDerivedStateFromProps](https://github.com/facebook/react/blob/25dda90c1ecb0c662ab06e2c80c1ee31e0ae9d36/packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js#L1035)
});
Expand Down