From 6d887aaf591cfa05d5fea978bbd87e3e502bfa86 Mon Sep 17 00:00:00 2001 From: edison Date: Mon, 6 Dec 2021 13:58:45 +0800 Subject: [PATCH] fix(runtime-core): handle initial undefined attrs (#5017) fix #5016 --- .../__tests__/componentProps.spec.ts | 22 +++++++++++++++++++ packages/runtime-core/src/componentProps.ts | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/componentProps.spec.ts b/packages/runtime-core/__tests__/componentProps.spec.ts index 863b12d0699..40725dfcd13 100644 --- a/packages/runtime-core/__tests__/componentProps.spec.ts +++ b/packages/runtime-core/__tests__/componentProps.spec.ts @@ -573,4 +573,26 @@ describe('component props', () => { render(h(Comp, { foo: null }), root) }).not.toThrow() }) + + // #5016 + test('handling attr with undefined value', () => { + const Comp = { + render(this: any) { + return JSON.stringify(this.$attrs) + Object.keys(this.$attrs) + } + } + const root = nodeOps.createElement('div') + + let attrs: any = { foo: undefined } + + render(h(Comp, attrs), root) + expect(serializeInner(root)).toBe( + JSON.stringify(attrs) + Object.keys(attrs) + ) + + render(h(Comp, (attrs = { foo: 'bar' })), root) + expect(serializeInner(root)).toBe( + JSON.stringify(attrs) + Object.keys(attrs) + ) + }) }) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index c999492225c..cf73261fc51 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -369,7 +369,7 @@ function setFullProps( continue } } - if (value !== attrs[key]) { + if (!(key in attrs) || value !== attrs[key]) { attrs[key] = value hasAttrsChanged = true }