Skip to content

Commit fff62e2

Browse files
authored
fix(runtime-core): non-stable Fragment should always unmount its children (#2445)
fix #2444
1 parent 0227b4a commit fff62e2

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/runtime-core/__tests__/rendererOptimizedMode.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,46 @@ describe('renderer: optimized mode', () => {
475475
render(null, root)
476476
expect(spy).toHaveBeenCalledTimes(1)
477477
})
478+
479+
// #2444
480+
// `KEYED_FRAGMENT` and `UNKEYED_FRAGMENT` always need to diff its children
481+
test('non-stable Fragment always need to diff its children', () => {
482+
const spyA = jest.fn()
483+
const spyB = jest.fn()
484+
const ChildA = {
485+
setup() {
486+
onBeforeUnmount(spyA)
487+
return () => 'child'
488+
}
489+
}
490+
const ChildB = {
491+
setup() {
492+
onBeforeUnmount(spyB)
493+
return () => 'child'
494+
}
495+
}
496+
const Parent = () => (
497+
openBlock(),
498+
createBlock('div', null, [
499+
(openBlock(true),
500+
createBlock(
501+
Fragment,
502+
null,
503+
[createVNode(ChildA, { key: 0 })],
504+
128 /* KEYED_FRAGMENT */
505+
)),
506+
(openBlock(true),
507+
createBlock(
508+
Fragment,
509+
null,
510+
[createVNode(ChildB)],
511+
256 /* UNKEYED_FRAGMENT */
512+
))
513+
])
514+
)
515+
render(h(Parent), root)
516+
render(null, root)
517+
expect(spyA).toHaveBeenCalledTimes(1)
518+
expect(spyB).toHaveBeenCalledTimes(1)
519+
})
478520
})

packages/runtime-core/src/renderer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2038,7 +2038,12 @@ function baseCreateRenderer(
20382038
false,
20392039
true
20402040
)
2041-
} else if (!optimized && shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
2041+
} else if (
2042+
(type === Fragment &&
2043+
(patchFlag & PatchFlags.KEYED_FRAGMENT ||
2044+
patchFlag & PatchFlags.UNKEYED_FRAGMENT)) ||
2045+
(!optimized && shapeFlag & ShapeFlags.ARRAY_CHILDREN)
2046+
) {
20422047
unmountChildren(children as VNode[], parentComponent, parentSuspense)
20432048
}
20442049

0 commit comments

Comments
 (0)