Skip to content

Commit 55a0a20

Browse files
authored
fix(watch): traverse refs in deep watch (#753)
Co-authored-by: webfansplz <>
1 parent bc7c2af commit 55a0a20

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/apis/watch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ function createWatcher(
332332
)
333333
}
334334

335+
if (deep) {
336+
const baseGetter = getter
337+
getter = () => traverse(baseGetter())
338+
}
339+
335340
const applyCb = (n: any, o: any) => {
336341
// cleanup before running cb again
337342
runCleanup()

test/v3/runtime-core/apiWatch.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,23 @@ describe('api: watch', () => {
531531

532532
expect(data2.value).toMatchObject([1])
533533
})
534+
535+
it('watching deep ref', async () => {
536+
const count = ref(0)
537+
const double = computed(() => count.value * 2)
538+
const state = reactive([count, double])
539+
540+
let dummy
541+
watch(
542+
() => state,
543+
(state) => {
544+
dummy = [state[0].value, state[1].value]
545+
},
546+
{ deep: true }
547+
)
548+
549+
count.value++
550+
await nextTick()
551+
expect(dummy).toEqual([1, 2])
552+
})
534553
})

0 commit comments

Comments
 (0)