Skip to content

Commit

Permalink
Fix components that use sCU and return Fragments (+4 B)
Browse files Browse the repository at this point in the history
Fixes #1415
  • Loading branch information
andrewiggins committed Apr 6, 2019
1 parent 9d5e90a commit b8bd7b4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export function diff(dom, parentDom, newVNode, oldVNode, context, isSvg, excessD
c.props = newVNode.props;
c.state = s;
c._dirty = false;
newVNode._lastDomChild = oldVNode._lastDomChild;
break outer;
}

Expand Down
69 changes: 69 additions & 0 deletions test/browser/fragments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1725,4 +1725,73 @@ describe('Fragment', () => {
li(1)
].join('')));
});

it('should properly render Components that return Fragments and use shouldComponentUpdate #1415', () => {
class SubList extends Component {
shouldComponentUpdate(nextProps) {
return nextProps.prop1 !== this.props.prop1;
}
render() {
return (
<Fragment>
<div>2</div>
<div>3</div>
</Fragment>
);
}
}

/** @type {(update: any) => void} */
let setState;
class App extends Component {
constructor() {
super();
setState = update => this.setState(update);

this.state = { error: false };
}

render() {
return (
<div>
{this.state.error ? (
<div>Error!</div>
) : (
<div>
<div>1</div>
<SubList prop1={this.state.error} />
</div>
)}
</div>
);
}
}

const successHtml = div(div([
div(1),
div(2),
div(3)
].join('')));

const errorHtml = div(div('Error!'));

render(<App />, scratch);
expect(scratch.innerHTML).to.equal(successHtml);

setState({}); // Trigger sCU
rerender();
expect(scratch.innerHTML).to.equal(successHtml);

setState({ error: true });
rerender();
expect(scratch.innerHTML).to.equal(errorHtml);

setState({ error: false });
rerender();
expect(scratch.innerHTML).to.equal(successHtml);

setState({}); // Trigger sCU again
rerender();
expect(scratch.innerHTML).to.equal(successHtml);
});
});

0 comments on commit b8bd7b4

Please sign in to comment.