Skip to content

Commit

Permalink
avoid bailing in strict equality (#3884)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Feb 4, 2023
1 parent 6e2e952 commit 8a30bcc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,16 @@ export function diff(
) === false) ||
newVNode._original === oldVNode._original
) {
c.props = newProps;
c.state = c._nextState;
// More info about this here: https://gist.github.com/JoviDeCroock/bec5f2ce93544d2e6070ef8e0036e4e8
if (newVNode._original !== oldVNode._original) c._dirty = false;
if (newVNode._original !== oldVNode._original) {
// When we are dealing with a bail because of sCU we have to update
// the props, state and dirty-state.
// when we are dealing with strict-equality we don't as the child could still
// be dirtied see #3883
c.props = newProps;
c.state = c._nextState;
c._dirty = false;
}
newVNode._dom = oldVNode._dom;
newVNode._children = oldVNode._children;
newVNode._children.forEach(vnode => {
Expand Down
47 changes: 47 additions & 0 deletions test/browser/lifecycles/shouldComponentUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,53 @@ describe('Lifecycle methods', () => {
// ]);
});

it('should correctly handle double state updates', () => {
let updateParent, updateChild;
class Parent extends Component {
state = { text: 'parent-old' };

componentDidMount() {
updateParent = () => this.setState({ text: 'Parent-NEW' });
}

render() {
return (
<Fragment>
{this.props.children} and {this.state.text}
</Fragment>
);
}
}

class Child extends Component {
state = { text: 'child-old' };

shouldComponentUpdate(nextProps, nextState) {
return this.state.text !== nextState.text;
}

componentDidMount() {
updateChild = () => this.setState({ text: 'Child-NEW' });
}

render() {
return <h1>{this.state.text}</h1>;
}
}

render(
<Parent>
<Child />
</Parent>,
scratch
);

updateParent();
updateChild();
rerender();
expect(scratch.innerHTML).to.equal('<h1>Child-NEW</h1> and Parent-NEW');
});

it('should maintain the order if memoised component initially rendered empty content', () => {
let showText, updateParent;
class Child extends Component {
Expand Down

0 comments on commit 8a30bcc

Please sign in to comment.