Skip to content

Add: Callback Flush Timing - About the flush: sync #1798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
51 changes: 51 additions & 0 deletions src/guide/essentials/watchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,57 @@ watchPostEffect(() => {

</div>

The `flush: 'pre'|'post'` option buffers the callback so that it is only called once at the end, even if there are multiple state changes in the same "tick".

The `flush: 'sync'` option should be used if the callback needs to be invoked synchronously on every state change multiple times within the same tick.

<div class="options-api">

```js
export default {
data: () => ({ count: 0 }),
watch: {
count: {
handler(val, preVal) {
console.log('Changed:', val, preVal)
},
flush: 'sync'
}
},
methods: {
increment() {
this.count++
// then trigger handler
this.count++
// then trigger handler
this.count++
// then trigger handler
}
}
}
```

</div>

<div class="composition-api">

```js
const count = ref(0)
const callback = (val, preVal) => console.log('Changed:', val, preVal)
const options = { flush: 'sync' }

watch(count, callback, options)

count.value++
// then trigger callback
count.value++
// then trigger callback
count.value++
// then trigger callback
```

</div>

<div class="options-api">

## `this.$watch()` \*
Expand Down