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 shallow renderer set instance state after gDSFP before calling sCU #14613

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Unwind abstraction
  • Loading branch information
gaearon committed Jan 18, 2019
commit 94b6f8ce9a90da5b70092cecdedf52cb82bfe1b0
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ describe('ReactComponentLifeCycle', () => {
expect(log).toEqual([]);
});

it('should pass old state to shouldComponentUpdate even getDerivedStateFromProps return new state', () => {
it('should pass previous state to shouldComponentUpdate even with getDerivedStateFromProps', () => {
const divRef = React.createRef();
class SimpleComponent extends React.Component {
constructor(props) {
Expand Down
51 changes: 23 additions & 28 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,17 @@ class ReactShallowRenderer {
this._updater,
);

const updated = this._updateNewStateFromStaticLifecycle(element.props);
if (updated) {
this._instance.state = this._newState;
if (typeof element.type.getDerivedStateFromProps === 'function') {
const oldState = this._instance.state;
const partialState = element.type.getDerivedStateFromProps.call(
null,
element.props,
oldState,
);
if (partialState != null) {
const newState = Object.assign({}, oldState, partialState);
this._instance.state = newState;
}
}

if (element.type.hasOwnProperty('contextTypes')) {
Expand Down Expand Up @@ -656,10 +664,19 @@ class ReactShallowRenderer {
}
}
}
this._updateNewStateFromStaticLifecycle(props);

// Read state after cWRP in case it calls setState
const state = this._newState || oldState;
let state = this._newState || oldState;
if (typeof type.getDerivedStateFromProps === 'function') {
const partialState = type.getDerivedStateFromProps.call(
null,
props,
state,
);
if (partialState != null) {
state = Object.assign({}, state, partialState);
}
}

let shouldUpdate = true;
if (this._forcedUpdate) {
Expand Down Expand Up @@ -695,36 +712,14 @@ class ReactShallowRenderer {
this._instance.context = context;
this._instance.props = props;
this._instance.state = state;
this._newState = null;

if (shouldUpdate) {
this._rendered = this._instance.render();
}
// Intentionally do not call componentDidUpdate()
// because DOM refs are not available.
}

_updateNewStateFromStaticLifecycle(props: Object) {
if (this._element === null) {
return;
}
const {type} = this._element;

if (typeof type.getDerivedStateFromProps === 'function') {
const oldState = this._newState || this._instance.state;
const partialState = type.getDerivedStateFromProps.call(
null,
props,
oldState,
);

if (partialState != null) {
const newState = Object.assign({}, oldState, partialState);
this._newState = newState;
return true;
}
}
return false;
}
}

let currentlyValidatingElement = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ describe('ReactShallowRenderer', () => {
expect(result).toEqual(<div>value:1</div>);
});

it('should pass old state to shouldComponentUpdate even getDerivedStateFromProps return new state', () => {
it('should pass previous state to shouldComponentUpdate even with getDerivedStateFromProps', () => {
class SimpleComponent extends React.Component {
constructor(props) {
super(props);
Expand Down