Skip to content

Commit

Permalink
avoid crasshing when replacing existing node
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Feb 16, 2024
1 parent e542965 commit d1f8ee5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
5 changes: 1 addition & 4 deletions hooks/test/browser/combinations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ describe('combinations', () => {
]);
});

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

let update;
Expand Down Expand Up @@ -507,17 +507,14 @@ describe('combinations', () => {
expect(scratch.innerHTML).to.equal('<div><div>B</div><div></div></div>');

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

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

update();
console.log('--- RENDER');
rerender();
expect(scratch.innerHTML).to.equal('<div><div></div><div>B</div></div>');
});
Expand Down
16 changes: 6 additions & 10 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ export function diffChildren(

// Adjust DOM nodes
newDom = childVNode._dom;
console.log(
'compared',
newDom,
childVNode._index,
childVNode && childVNode.type,
oldVNode && oldVNode.type
);
if (childVNode.ref && oldVNode.ref != childVNode.ref) {
if (oldVNode.ref) {
applyRef(oldVNode.ref, null, childVNode);
Expand All @@ -119,7 +112,6 @@ export function diffChildren(
firstChildDom = newDom;
}

console.log(newDom, childVNode._flags & INSERT_VNODE);
if (
childVNode._flags & INSERT_VNODE ||
oldVNode._children === childVNode._children
Expand Down Expand Up @@ -238,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

Check failure on line 237 in src/diff/children.js

View workflow job for this annotation

GitHub Actions / Build & Test

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

Check failure on line 237 in src/diff/children.js

View workflow job for this annotation

GitHub Actions / Build & Test / Build & Test

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
) {
if (oldVNode._dom == newParentVNode._nextDom) {
newParentVNode._nextDom = getDomSibling(oldVNode);
}
console.log('unmounting', oldVNode.type);
unmount(oldVNode, oldVNode, false);

// Explicitly nullify this position in oldChildren instead of just
Expand Down

0 comments on commit d1f8ee5

Please sign in to comment.