Skip to content

Commit

Permalink
fix and add test for 4283
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Feb 17, 2024
1 parent a003d42 commit 00b71c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
// assume DOM nodes in this subtree are mounted and usable.
oldChildren[i] = null;
remainingOldChildren--;
skew++;
}
continue;
}
Expand Down
53 changes: 53 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1356,4 +1356,57 @@ describe('render()', () => {
rerender();
expect(scratch.innerHTML).to.equal('<div><div></div><div>B</div></div>');
});

it('should not crash or repeatedly add the same child when replacing a matched vnode with null (mixed dom-types)', () => {
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>
<span />
{null}
<B />
<div />
</div>
);
}
}

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

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

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

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

0 comments on commit 00b71c4

Please sign in to comment.