Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,13 @@ describe('api: watch', () => {
oldValue: 2
})
})

// #683
it('watching undefined', async () => {
let dummy = null
const r = ref(undefined)
watch(r, v => (dummy = v))
await nextTick()
expect(dummy).toBe(undefined)
})
})
6 changes: 5 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,18 @@ function doWatch(
}
}

let oldValue = isArray(source) ? [] : undefined
const watchDefaultSymbol = (Symbol('watchDefault') as unknown) as undefined // Unique symbol for default, see #683
let oldValue = isArray(source) ? [] : watchDefaultSymbol
const applyCb = cb
? () => {
if (instance && instance.isUnmounted) {
return
}
const newValue = runner()
if (deep || hasChanged(newValue, oldValue)) {
if (oldValue === watchDefaultSymbol) {
oldValue = undefined
}
// cleanup before running cb again
if (cleanup) {
cleanup()
Expand Down