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