Skip to content

Commit

Permalink
fix(runtime-core): do not emit when defineModel ref is set with same …
Browse files Browse the repository at this point in the history
…value (#11162)

close #11125
  • Loading branch information
yangxiuxiu1115 committed Jul 17, 2024
1 parent 3e9e32e commit f1bb0ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
40 changes: 40 additions & 0 deletions packages/runtime-core/__tests__/helpers/useModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
6 changes: 4 additions & 2 deletions packages/runtime-core/src/helpers/useModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export function useModel(
},

set(value) {
if (!hasChanged(value, localValue)) {
return
}
const rawProps = i.vnode!.props
if (
!(
Expand All @@ -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
Expand Down

0 comments on commit f1bb0ae

Please sign in to comment.