---
title: Use flush post for watchEffect with Template Refs
impact: MEDIUM
impactDescription: Default watchEffect runs before DOM updates, causing refs to be out of sync
type: gotcha
tags: [vue3, watchEffect, template-refs, flush, dom-timing]
---
# Use flush post for watchEffect with Template Refs
**Impact: MEDIUM** - By default, `watchEffect` runs before the DOM is updated. When watching template refs, this means the effect may run with stale or null ref values. Use `flush: 'post'` to ensure the effect runs after DOM updates when refs are current.
This timing issue is particularly confusing because the watcher runs, but the ref doesn't yet reflect the current DOM state.
## Task Checklist
- [ ] Use `{ flush: 'post' }` when watchEffect accesses template refs
- [ ] Alternatively, use `watchPostEffect` helper for cleaner syntax
- [ ] Still include null checks as refs can be unmounted
- [ ] Consider using `watch` with explicit ref watching instead
**Incorrect:**
```vue
```
```vue
{{ item }}
```
**Correct:**
```vue
```
```vue
```
```vue
```
```vue
```
## Flush Options Explained
```javascript
// Default: 'pre' - runs before DOM update
watchEffect(() => { ... }) // Same as { flush: 'pre' }
// 'post' - runs after DOM update (use for refs)
watchEffect(() => { ... }, { flush: 'post' })
watchPostEffect(() => { ... }) // Shorthand
// 'sync' - runs synchronously (rarely needed, can cause issues)
watchEffect(() => { ... }, { flush: 'sync' })
watchSyncEffect(() => { ... }) // Shorthand
```
## When to Use Each Flush Mode
| Scenario | Recommended Flush |
|----------|-------------------|
| Accessing template refs | `post` |
| Reading updated DOM | `post` |
| Triggering before render | `pre` (default) |
| Performance-critical sync updates | `sync` (with caution) |
## Reference
- [Vue.js Watchers - Callback Flush Timing](https://vuejs.org/guide/essentials/watchers.html#callback-flush-timing)
- [Vue.js watchEffect API](https://vuejs.org/api/reactivity-core.html#watcheffect)