Skip to content

Commit f1bb0ae

Browse files
fix(runtime-core): do not emit when defineModel ref is set with same value (#11162)
close #11125
1 parent 3e9e32e commit f1bb0ae

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

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

+40
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,44 @@ describe('useModel', () => {
612612
// should not force local update if set to the same value
613613
expect(compRender).toHaveBeenCalledTimes(3)
614614
})
615+
616+
test('set no change value', async () => {
617+
let changeChildMsg: (() => void) | null = null
618+
619+
const compRender = vi.fn()
620+
const Comp = defineComponent({
621+
props: ['msg'],
622+
emits: ['update:msg'],
623+
setup(props) {
624+
const childMsg = useModel(props, 'msg')
625+
changeChildMsg = () => {
626+
childMsg.value = childMsg.value
627+
}
628+
return () => {
629+
return childMsg.value
630+
}
631+
},
632+
})
633+
634+
const msg = ref('HI')
635+
const Parent = defineComponent({
636+
setup() {
637+
return () =>
638+
h(Comp, {
639+
msg: msg.value,
640+
'onUpdate:msg': val => {
641+
msg.value = val
642+
compRender()
643+
},
644+
})
645+
},
646+
})
647+
648+
const root = nodeOps.createElement('div')
649+
render(h(Parent), root)
650+
651+
expect(compRender).toBeCalledTimes(0)
652+
changeChildMsg!()
653+
expect(compRender).toBeCalledTimes(0)
654+
})
615655
})

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export function useModel(
5151
},
5252

5353
set(value) {
54+
if (!hasChanged(value, localValue)) {
55+
return
56+
}
5457
const rawProps = i.vnode!.props
5558
if (
5659
!(
@@ -62,8 +65,7 @@ export function useModel(
6265
(`onUpdate:${name}` in rawProps ||
6366
`onUpdate:${camelizedName}` in rawProps ||
6467
`onUpdate:${hyphenatedName}` in rawProps)
65-
) &&
66-
hasChanged(value, localValue)
68+
)
6769
) {
6870
// no v-model, local update
6971
localValue = value

0 commit comments

Comments
 (0)