]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): do not emit when defineModel ref is set with same value (#11162)
authoryangxiuxiu <79584569+yangxiuxiu1115@users.noreply.github.com>
Wed, 17 Jul 2024 08:46:10 +0000 (16:46 +0800)
committerGitHub <noreply@github.com>
Wed, 17 Jul 2024 08:46:10 +0000 (16:46 +0800)
close #11125

packages/runtime-core/__tests__/helpers/useModel.spec.ts
packages/runtime-core/src/helpers/useModel.ts

index f5b2a0108b003c63e8e8cf392d87a0d50e71a51c..097e52f916768ff66c6ee03b297b0971c1c5de17 100644 (file)
@@ -612,4 +612,44 @@ describe('useModel', () => {
     // should not force local update if set to the same value
     expect(compRender).toHaveBeenCalledTimes(3)
   })
+
+  test('set no change value', async () => {
+    let changeChildMsg: (() => void) | null = null
+
+    const compRender = vi.fn()
+    const Comp = defineComponent({
+      props: ['msg'],
+      emits: ['update:msg'],
+      setup(props) {
+        const childMsg = useModel(props, 'msg')
+        changeChildMsg = () => {
+          childMsg.value = childMsg.value
+        }
+        return () => {
+          return childMsg.value
+        }
+      },
+    })
+
+    const msg = ref('HI')
+    const Parent = defineComponent({
+      setup() {
+        return () =>
+          h(Comp, {
+            msg: msg.value,
+            'onUpdate:msg': val => {
+              msg.value = val
+              compRender()
+            },
+          })
+      },
+    })
+
+    const root = nodeOps.createElement('div')
+    render(h(Parent), root)
+
+    expect(compRender).toBeCalledTimes(0)
+    changeChildMsg!()
+    expect(compRender).toBeCalledTimes(0)
+  })
 })
index 8e775a4b90e1aad0634d60c2b5bbaf6ee2411a18..493264ea73a0308347973e41fa507a135c48788f 100644 (file)
@@ -51,6 +51,9 @@ export function useModel(
       },
 
       set(value) {
+        if (!hasChanged(value, localValue)) {
+          return
+        }
         const rawProps = i.vnode!.props
         if (
           !(
@@ -62,8 +65,7 @@ export function useModel(
             (`onUpdate:${name}` in rawProps ||
               `onUpdate:${camelizedName}` in rawProps ||
               `onUpdate:${hyphenatedName}` in rawProps)
-          ) &&
-          hasChanged(value, localValue)
+          )
         ) {
           // no v-model, local update
           localValue = value