]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): handle initial undefined attrs (#5017)
authoredison <daiwei521@126.com>
Mon, 6 Dec 2021 05:58:45 +0000 (13:58 +0800)
committerGitHub <noreply@github.com>
Mon, 6 Dec 2021 05:58:45 +0000 (00:58 -0500)
fix #5016

packages/runtime-core/__tests__/componentProps.spec.ts
packages/runtime-core/src/componentProps.ts

index 863b12d0699095d5d9ee0eb797d97afbda076390..40725dfcd135508d8833c37c46690c3892d51643 100644 (file)
@@ -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)
+    )
+  })
 })
index c999492225c02525885178efece68a45c6d71ded..cf73261fc51ae0db1b21019abf8d9c08748480d0 100644 (file)
@@ -369,7 +369,7 @@ function setFullProps(
             continue
           }
         }
-        if (value !== attrs[key]) {
+        if (!(key in attrs) || value !== attrs[key]) {
           attrs[key] = value
           hasAttrsChanged = true
         }