Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export function diffChildren(

for (i = 0; i < newChildrenLength; i++) {
childVNode = newParentVNode._children[i];

if (
childVNode == null ||
typeof childVNode == 'boolean' ||
Expand Down Expand Up @@ -231,11 +230,15 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
// Handle unmounting null placeholders, i.e. VNode => null in unkeyed children
if (childVNode == null) {
oldVNode = oldChildren[i];
if (oldVNode && oldVNode.key == null && oldVNode._dom) {
if (
oldVNode &&
oldVNode.key == null &&
oldVNode._dom &&
(oldVNode._flags & MATCHED) === 0
) {
if (oldVNode._dom == newParentVNode._nextDom) {
newParentVNode._nextDom = getDomSibling(oldVNode);
}

unmount(oldVNode, oldVNode, false);

// Explicitly nullify this position in oldChildren instead of just
Expand All @@ -250,7 +253,6 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
oldChildren[i] = null;
remainingOldChildren--;
}

continue;
}

Expand Down
48 changes: 48 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,4 +1308,52 @@ describe('render()', () => {
expect(divs[1].hasAttribute('role')).to.equal(false);
expect(divs[2].hasAttribute('role')).to.equal(false);
});

it('should not crash or repeatedly add the same child when replacing a matched vnode with null', () => {
const B = () => <div>B</div>;

let update;
class App extends Component {
constructor(props) {
super(props);
this.state = { show: true };
update = () => {
this.setState(state => ({ show: !state.show }));
};
}

render() {
if (this.state.show) {
return (
<div>
<B />
<div />
</div>
);
}
return (
<div>
<div />
{null}
<B />
</div>
);
}
}

render(<App />, scratch);
expect(scratch.innerHTML).to.equal('<div><div>B</div><div></div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div></div><div>B</div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div>B</div><div></div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div></div><div>B</div></div>');
});
});