Match-id-91b148af957f0c2f4043f45a93a8a6e8929a0507

This commit is contained in:
* 2022-09-07 11:27:22 +08:00 committed by *
parent 5bc66a4e69
commit ec83aff5e6
1 changed files with 10 additions and 6 deletions

View File

@ -55,7 +55,7 @@ function deleteVNodes(parentVNode: VNode, startDelVNode: VNode | null, endVNode?
}
}
function checkCanReuseNode(oldNode: VNode | null, newChild: any): boolean {
function checkCanReuseNode(oldNode: VNode | null, newChild: any, newNodeIdx: number): boolean {
if (newChild === null) {
return false;
}
@ -70,7 +70,12 @@ function checkCanReuseNode(oldNode: VNode | null, newChild: any): boolean {
return oldKey === null;
}
if (newChild.vtype === TYPE_COMMON_ELEMENT || newChild.vtype === TYPE_PORTAL) {
return oldKey === newChild.key;
if(oldKey || newChild.key) {
return oldKey === newChild.key;
} else {
// 新旧节点的index应该相同才能复用null会影响位置
return oldNode?.eIndex === newNodeIdx;
}
}
}
@ -254,7 +259,7 @@ function diffArrayNodesHandler(
nextOldNode = oldNode.next;
}
canBeReuse = checkCanReuseNode(oldNode, newChildren[leftIdx]);
canBeReuse = checkCanReuseNode(oldNode, newChildren[leftIdx], leftIdx);
// 不能复用break
if (!canBeReuse) {
oldNode = oldNode ?? nextOldNode;
@ -294,9 +299,8 @@ function diffArrayNodesHandler(
if (rightOldIndex < 0 || rightOldNode === null) {
break;
}
// 新旧节点的index应该相同才能复用null会影响位置
const samePosition = rightOldNode.eIndex === rightIdx - 1;
canBeReuse = checkCanReuseNode(rightOldNode, newChildren[rightIdx - 1]) && samePosition;
canBeReuse = checkCanReuseNode(rightOldNode, newChildren[rightIdx - 1], rightIdx - 1);
// 不能复用break
if (!canBeReuse) {
break;