From 5c5cd6ecdf2153ce0fdf9103d50259714e8852d2 Mon Sep 17 00:00:00 2001 From: daiwei Date: Mon, 29 Nov 2021 19:23:08 +0800 Subject: [PATCH 1/2] fix(runtime-core): prop assigned to undefined also need to put into attrs fix(runtime-core): prop assigned to undefined also need to put into attrs chore: format --- packages/runtime-core/src/componentProps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 924f40a7384..cebf3423c39 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -368,7 +368,7 @@ function setFullProps( continue } } - if (value !== attrs[key]) { + if (!(key in attrs) || value !== attrs[key]) { attrs[key] = value hasAttrsChanged = true } From 2405dcc5c5cb430c72666b6a6c71069407541fc3 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 6 Dec 2021 12:50:14 +0800 Subject: [PATCH 2/2] test: add test --- .../__tests__/componentProps.spec.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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) + ) + }) })