Skip to content

Commit ded5ab7

Browse files
authored
refactor: watch APIs default to trigger pre-flush (#566)
BREAKING CHANGE: watch APIs now default to use `flush: 'pre'` instead of `flush: 'post'`. - Check vuejs/core@49bb447 - This change affects `watch`, `watchEffect`, the `watch` component option, and `this.$watch`. - As pointed out by @skirtles-code in [this comment](#1706 (comment)), Vue 2's watch behavior is pre-flush, and the ecosystem has many uses of watch that assumes the pre-flush behavior. Defaulting to post-flush can result in unnecessary re-renders without the users being aware of it. - With this change, watchers need to specify `{ flush: 'post' }` via options to trigger callback after Vue render updates. Note that specifying `{ flush: 'post' }` will also defer `watchEffect`'s initial run to wait for the component's initial render.
1 parent d10a0ce commit ded5ab7

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/apis/watch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function getWatcherOption(options?: Partial<WatchOptions>): WatchOptions {
8383
...{
8484
immediate: false,
8585
deep: false,
86-
flush: 'post',
86+
flush: 'pre',
8787
},
8888
...options,
8989
}
@@ -94,7 +94,7 @@ function getWatchEffectOption(options?: Partial<WatchOptions>): WatchOptions {
9494
...{
9595
immediate: true,
9696
deep: false,
97-
flush: 'post',
97+
flush: 'pre',
9898
},
9999
...options,
100100
}

test/apis/watch.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe('api/watch', () => {
178178
spy(newVal, oldVal)
179179
rerenderedText = vm.$el.textContent
180180
},
181-
{ lazy: true }
181+
{ lazy: true, flush: 'post' }
182182
)
183183
return {
184184
a,
@@ -210,7 +210,7 @@ describe('api/watch', () => {
210210
rerenderedText = vm.$el.textContent
211211
}
212212
},
213-
{ immediate: true }
213+
{ immediate: true, flush: 'post' }
214214
)
215215
return {
216216
a,

0 commit comments

Comments
 (0)