Skip to content
Draft
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
19 changes: 12 additions & 7 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ export function diffChildren(

let newChildrenLength = renderResult.length;

let newChildren = new Array(newChildrenLength);
newParentVNode._children = newChildren;

oldDom = constructNewChildrenArray(
newParentVNode,
newChildren,
renderResult,
oldChildren,
oldDom
);

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

// At this point, constructNewChildrenArray has assigned _index to be the
Expand Down Expand Up @@ -144,11 +148,13 @@ export function diffChildren(

/**
* @param {VNode} newParentVNode
* @param {VNode[]} newChildren
* @param {ComponentChildren[]} renderResult
* @param {VNode[]} oldChildren
*/
function constructNewChildrenArray(
newParentVNode,
newChildren,
renderResult,
oldChildren,
oldDom
Expand All @@ -166,7 +172,6 @@ function constructNewChildrenArray(

let skew = 0;

newParentVNode._children = new Array(newChildrenLength);
for (i = 0; i < newChildrenLength; i++) {
// @ts-expect-error We are reusing the childVNode variable to hold both the
// pre and post normalized childVNode
Expand All @@ -177,7 +182,7 @@ function constructNewChildrenArray(
typeof childVNode == 'boolean' ||
typeof childVNode == 'function'
) {
childVNode = newParentVNode._children[i] = null;
newChildren[i] = null;
continue;
}
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
Expand All @@ -190,15 +195,15 @@ function constructNewChildrenArray(
typeof childVNode == 'bigint' ||
childVNode.constructor == String
) {
childVNode = newParentVNode._children[i] = createVNode(
childVNode = newChildren[i] = createVNode(
null,
childVNode,
null,
null,
null
);
} else if (isArray(childVNode)) {
childVNode = newParentVNode._children[i] = createVNode(
childVNode = newChildren[i] = createVNode(
Fragment,
{ children: childVNode },
null,
Expand All @@ -210,15 +215,15 @@ function constructNewChildrenArray(
// scenario:
// const reuse = <div />
// <div>{reuse}<span />{reuse}</div>
childVNode = newParentVNode._children[i] = createVNode(
childVNode = newChildren[i] = createVNode(
childVNode.type,
childVNode.props,
childVNode.key,
childVNode.ref ? childVNode.ref : null,
childVNode._original
);
} else {
childVNode = newParentVNode._children[i] = childVNode;
childVNode = newChildren[i] = childVNode;
}

const skewedIndex = i + skew;
Expand Down
Loading