Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/watch-flush-timing.md
1---2title: Use flush post When Accessing Updated DOM in Watchers3impact: MEDIUM4impactDescription: Default watcher timing runs before DOM updates, causing stale DOM reads5type: capability6tags: [vue3, watch, watchers, flush, DOM, timing, post]7---89# Use flush: 'post' When Accessing Updated DOM in Watchers1011**Impact: MEDIUM** - By default, watcher callbacks run before the component's DOM is updated. If you access the DOM in a watcher callback, you'll see the pre-update state. Use `flush: 'post'` or `watchPostEffect` when you need to access the updated DOM.1213## Task Checklist1415- [ ] Use `{ flush: 'post' }` when reading DOM after reactive state changes16- [ ] Use `watchPostEffect()` as a shorthand for `watchEffect` with flush: 'post'17- [ ] Avoid `{ flush: 'sync' }` unless absolutely necessary (performance impact)18- [ ] Remember default timing is ideal for most non-DOM operations1920**Incorrect:**21```vue22<script setup>23import { ref, watch, watchEffect } from 'vue'2425const count = ref(0)26const listItems = ref(['a', 'b', 'c'])2728// BAD: DOM shows old value when this runs29watch(count, () => {30// Element still shows the OLD count value31const el = document.querySelector('.counter')32console.log('DOM shows:', el.textContent) // Old value!33})3435// BAD: List DOM not yet updated36watchEffect(() => {37console.log('Items:', listItems.value.length)38// DOM still has old number of list items39const items = document.querySelectorAll('.list-item')40console.log('DOM items:', items.length) // Old count!41})42</script>4344<template>45<div class="counter">{{ count }}</div>46<ul>47<li v-for="item in listItems" :key="item" class="list-item">48{{ item }}49</li>50</ul>51</template>52```5354**Correct:**55```vue56<script setup>57import { ref, watch, watchEffect, watchPostEffect } from 'vue'5859const count = ref(0)60const listItems = ref(['a', 'b', 'c'])6162// CORRECT: flush: 'post' runs after DOM update63watch(64count,65() => {66const el = document.querySelector('.counter')67console.log('DOM shows:', el.textContent) // Correct new value!68},69{ flush: 'post' }70)7172// CORRECT: watchPostEffect shorthand73watchPostEffect(() => {74console.log('Items:', listItems.value.length)75const items = document.querySelectorAll('.list-item')76console.log('DOM items:', items.length) // Matches listItems.length!77})7879// CORRECT: Using watchEffect with flush option80watchEffect(81() => {82// Access reactive state and DOM together83const expectedCount = listItems.value.length84const actualCount = document.querySelectorAll('.list-item').length85console.log(`Expected: ${expectedCount}, Actual: ${actualCount}`)86},87{ flush: 'post' }88)89</script>9091<template>92<div class="counter">{{ count }}</div>93<ul>94<li v-for="item in listItems" :key="item" class="list-item">95{{ item }}96</li>97</ul>98</template>99```100101## Flush Timing Options102103```javascript104import { watch, watchEffect, watchPostEffect, watchSyncEffect } from 'vue'105106// Default: 'pre' - runs before component DOM update107watch(source, callback) // Same as { flush: 'pre' }108109// Post: runs after component DOM update110watch(source, callback, { flush: 'post' })111watchPostEffect(callback) // Shorthand112113// Sync: runs immediately when reactive value changes114// USE WITH CAUTION - no batching, fires on every mutation115watch(source, callback, { flush: 'sync' })116watchSyncEffect(callback) // Shorthand117```118119## When to Use Each Flush Timing120121| Timing | Use Case |122|--------|----------|123| `'pre'` (default) | Logic that doesn't need DOM access |124| `'post'` | Reading or measuring updated DOM |125| `'sync'` | Debug logging, simple boolean flags only |126127## Sync Watcher Warning128129```javascript130import { ref, watch } from 'vue'131132const items = ref([1, 2, 3])133134// DANGEROUS: Fires for EVERY array mutation135watch(136items,137() => {138console.log('Changed!') // Called 3 times for push, push, push139},140{ flush: 'sync' }141)142143// This triggers the watcher 3 times synchronously144items.value.push(4)145items.value.push(5)146items.value.push(6)147148// Better: Use default flush which batches updates149watch(items, () => {150console.log('Changed!') // Called once after all mutations151}, { deep: true })152```153154## Practical Example: Auto-scroll155156```vue157<script setup>158import { ref, watchPostEffect } from 'vue'159160const messages = ref([])161const containerRef = ref(null)162163// Auto-scroll to bottom when new messages arrive164watchPostEffect(() => {165// Access messages.value to track it166const msgCount = messages.value.length167168// DOM is updated, safe to scroll169if (containerRef.value && msgCount > 0) {170containerRef.value.scrollTop = containerRef.value.scrollHeight171}172})173174function addMessage(text) {175messages.value.push({ text, timestamp: Date.now() })176}177</script>178179<template>180<div ref="containerRef" class="messages">181<div v-for="msg in messages" :key="msg.timestamp">182{{ msg.text }}183</div>184</div>185</template>186```187188## Reference189- [Vue.js Watchers - Callback Flush Timing](https://vuejs.org/guide/essentials/watchers.html#callback-flush-timing)190