From b3c3e52979d6d4b0312a3257474c15e903096062 Mon Sep 17 00:00:00 2001 From: haiweilian Date: Tue, 20 Dec 2022 14:22:40 +0800 Subject: [PATCH] fix(runtime-core): delete undefined stale slots --- .../__tests__/componentSlots.spec.ts | 28 +++++++++++++++++++ packages/runtime-core/src/componentSlots.ts | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/componentSlots.spec.ts b/packages/runtime-core/__tests__/componentSlots.spec.ts index 9248a8e6afe..fa6681a7125 100644 --- a/packages/runtime-core/__tests__/componentSlots.spec.ts +++ b/packages/runtime-core/__tests__/componentSlots.spec.ts @@ -167,6 +167,34 @@ describe('component: slots', () => { expect(instance.slots.footer()).toMatchObject([normalizeVNode('footer')]) }) + test('updateSlots: instance.slots should be updated correctly (when slotType becomes null)', async () => { + const flag1 = ref(true) + + let instance: any + const Child = () => { + instance = getCurrentInstance() + return 'child' + } + + const Comp = { + setup() { + return () => [ + h(Child, null, { + default: flag1.value ? () => 'default' : null + }) + ] + } + } + render(h(Comp), nodeOps.createElement('div')) + + expect(instance.slots).toHaveProperty('default') + + flag1.value = false + await nextTick() + + expect(instance.slots).not.toHaveProperty('default') + }) + test('updateSlots: instance.slots should be update correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)', async () => { const flag1 = ref(true) diff --git a/packages/runtime-core/src/componentSlots.ts b/packages/runtime-core/src/componentSlots.ts index 682a107e676..dc948694f7e 100644 --- a/packages/runtime-core/src/componentSlots.ts +++ b/packages/runtime-core/src/componentSlots.ts @@ -204,7 +204,7 @@ export const updateSlots = ( // delete stale slots if (needDeletionCheck) { for (const key in slots) { - if (!isInternalKey(key) && !(key in deletionComparisonTarget)) { + if (!isInternalKey(key) && deletionComparisonTarget[key] == null) { delete slots[key] } }