Skip to content

Commit c69c4bb

Browse files
authored
fix(watch): update oldValue before running cb to prevent stale value (#12296)
close #12294
1 parent 1a66474 commit c69c4bb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/reactivity/__tests__/watch.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,16 @@ describe('watch', () => {
277277

278278
expect(dummy).toEqual([1, 2, 3])
279279
})
280+
281+
test('watch with immediate reset and sync flush', () => {
282+
const value = ref(false)
283+
284+
watch(value, () => {
285+
value.value = false
286+
})
287+
288+
value.value = true
289+
value.value = true
290+
expect(value.value).toBe(false)
291+
})
280292
})

packages/reactivity/src/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ export function watch(
264264
: oldValue,
265265
boundCleanup,
266266
]
267+
oldValue = newValue
267268
call
268269
? call(cb!, WatchErrorCodes.WATCH_CALLBACK, args)
269270
: // @ts-expect-error
270271
cb!(...args)
271-
oldValue = newValue
272272
} finally {
273273
activeWatcher = currentWatcher
274274
}

0 commit comments

Comments
 (0)