Skip to content

Commit bb8dac4

Browse files
committed
docs: more tweaks on watch flush timing
1 parent 190ab87 commit bb8dac4

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

src/api/computed-watch-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function watchEffect(
6565
): StopHandle
6666
6767
interface WatchEffectOptions {
68-
flush?: 'pre' | 'post' | 'sync'
68+
flush?: 'pre' | 'post' | 'sync' // default: 'pre'
6969
onTrack?: (event: DebuggerEvent) => void
7070
onTrigger?: (event: DebuggerEvent) => void
7171
}

src/guide/reactivity-computed-watchers.md

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,9 @@ In this example:
122122
- The count will be logged synchronously on initial run.
123123
- When `count` is mutated, the callback will be called **before** the component has updated.
124124

125-
Note the first run is executed before the component is mounted. So if you wish to access the DOM (or template refs) in a watched effect, do it in the `onMounted` hook:
125+
In cases where a watcher effect needs to be re-run **after** component updates, we can pass an additional `options` object with the `flush` option (default is `'pre'`):
126126

127127
```js
128-
onMounted(() => {
129-
watchEffect(() => {
130-
// access the DOM or template refs
131-
})
132-
})
133-
```
134-
135-
In cases where a watcher effect needs to be re-run synchronously or after component updates, we can pass an additional `options` object with the `flush` option (default is `'pre'`):
136-
137-
```js
138-
// fire synchronously
139-
watchEffect(
140-
() => {
141-
/* ... */
142-
},
143-
{
144-
flush: 'sync'
145-
}
146-
)
147-
148128
// fire after component updates so you can access the updated DOM
149129
// Note: this will also defer the initial run of the effect until the
150130
// component's first render is finished.
@@ -158,6 +138,8 @@ watchEffect(
158138
)
159139
```
160140

141+
The `flush` option also accepts `'sync'`, which forces the effect to always trigger synchronously. This is however inefficient and should be rarely needed.
142+
161143
### Watcher Debugging
162144

163145
The `onTrack` and `onTrigger` options can be used to debug a watcher's behavior.

0 commit comments

Comments
 (0)