You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/essentials/watchers.md
+56-4Lines changed: 56 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -331,13 +331,17 @@ For examples like these, with only one dependency, the benefit of `watchEffect()
331
331
332
332
When you mutate reactive state, it may trigger both Vue component updates and watcher callbacks created by you.
333
333
334
-
By default, user-created watcher callbacks are called **before** Vue component updates. This means if you attempt to access the DOM inside a watcher callback, the DOM will be in the state before Vue has applied any updates.
334
+
Similar to component updates, user-created watcher callbacks are batched to avoid duplicate invocations. For example, we probably don't want a watcher to fire a thousand times if we synchronously push a thousand items into an array being watched.
335
335
336
-
If you want to access the DOM in a watcher callback **after** Vue has updated it, you need to specify the `flush: 'post'` option:
336
+
By default, a watcher's callback is called **after** parent component updates (if any), and **before** the owner component's DOM updates. This means if you attempt to access the owner component's own DOM inside a watcher callback, the DOM will be in a pre-update state.
337
+
338
+
### Post Watchers
339
+
340
+
If you want to access the owner component's DOM in a watcher callback **after** Vue has updated it, you need to specify the `flush: 'post'` option:
337
341
338
342
<divclass="options-api">
339
343
340
-
```js
344
+
```js{6}
341
345
export default {
342
346
// ...
343
347
watch: {
@@ -353,7 +357,7 @@ export default {
353
357
354
358
<divclass="composition-api">
355
359
356
-
```js
360
+
```js{2,6}
357
361
watch(source, callback, {
358
362
flush: 'post'
359
363
})
@@ -375,6 +379,54 @@ watchPostEffect(() => {
375
379
376
380
</div>
377
381
382
+
### Sync Watchers
383
+
384
+
It's also possible to create a watcher that fires synchronously, before any Vue-managed updates:
385
+
386
+
<divclass="options-api">
387
+
388
+
```js{6}
389
+
export default {
390
+
// ...
391
+
watch: {
392
+
key: {
393
+
handler() {},
394
+
flush: 'sync'
395
+
}
396
+
}
397
+
}
398
+
```
399
+
400
+
</div>
401
+
402
+
<divclass="composition-api">
403
+
404
+
```js{2,6}
405
+
watch(source, callback, {
406
+
flush: 'sync'
407
+
})
408
+
409
+
watchEffect(callback, {
410
+
flush: 'sync'
411
+
})
412
+
```
413
+
414
+
Sync `watchEffect()` also has a convenience alias, `watchSyncEffect()`:
415
+
416
+
```js
417
+
import { watchSyncEffect } from'vue'
418
+
419
+
watchSyncEffect(() => {
420
+
/* executed synchronously upon reactive data change */
421
+
})
422
+
```
423
+
424
+
</div>
425
+
426
+
:::warning Use with Caution
427
+
Sync watchers do not have batching and triggers every time a reactive mutation is detected. It's ok to use them to watch simple boolean values, but avoid using them on data sources that might be synchronously mutated many times, e.g. arrays.
0 commit comments