Skip to content

Commit

Permalink
fix(useVModel): clone set to true triggered infinite loop (#3097)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
motian and antfu committed Aug 8, 2023
1 parent 028a732 commit 48f4c6e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/core/useVModel/index.ts
@@ -1,6 +1,6 @@
import { isDef } from '@vueuse/shared'
import type { Ref, UnwrapRef, WritableComputedRef } from 'vue-demi'
import { computed, getCurrentInstance, isVue2, ref, watch } from 'vue-demi'
import { computed, getCurrentInstance, isVue2, nextTick, ref, watch } from 'vue-demi'
import type { CloneFn } from '../useCloned'
import { cloneFnJSON } from '../useCloned'

Expand Down Expand Up @@ -127,16 +127,23 @@ export function useVModel<P extends object, K extends keyof P, Name extends stri
if (passive) {
const initialValue = getValue()
const proxy = ref<P[K]>(initialValue!)
let isUpdating = false

watch(
() => props[key!],
v => (proxy as any).value = cloneFn(v) as UnwrapRef<P[K]>,
(v) => {
if (!isUpdating) {
isUpdating = true
;(proxy as any).value = cloneFn(v) as UnwrapRef<P[K]>
nextTick(() => isUpdating = false)
}
},
)

watch(
proxy,
(v) => {
if (v !== props[key!] || deep)
if (!isUpdating && (v !== props[key!] || deep))
triggerEmit(v as P[K])
},
{ deep },
Expand Down

0 comments on commit 48f4c6e

Please sign in to comment.