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/reactivity-same-tick-batching.md
1---2title: Understand Reactive Updates are Batched Per Event Loop Tick3impact: MEDIUM4impactDescription: Multiple synchronous reactive changes are batched - watchers only see the final value, not intermediate states5type: gotcha6tags: [vue3, reactivity, batching, event-loop, watchers, nextTick]7---89# Understand Reactive Updates are Batched Per Event Loop Tick1011**Impact: MEDIUM** - Vue batches multiple reactive state changes that happen synchronously within the same event loop tick. Watchers and computed properties only see the final state, not intermediate values. This is an optimization, but it can be surprising if you expect watchers to fire for each individual change.1213Understanding this behavior is essential for debugging scenarios where you expect to observe every state transition.1415## Task Checklist1617- [ ] Understand watchers fire once per tick with final value, not for each mutation18- [ ] Use `nextTick()` if you need to ensure DOM updates between state changes19- [ ] Use `flush: 'sync'` on watchers only if you absolutely need immediate execution20- [ ] For intermediate value tracking, consider logging or explicit state snapshots2122**Example of batching behavior:**23```javascript24import { ref, watch } from 'vue'2526const count = ref(0)2728watch(count, (newValue) => {29console.log('Count changed to:', newValue)30})3132// Multiple synchronous changes in the same tick33function multipleUpdates() {34count.value = 135count.value = 236count.value = 337count.value = 438}3940multipleUpdates()41// Console output: "Count changed to: 4"42// NOT: 1, 2, 3, 4 - only the final value is observed!43```4445**The console logs you WON'T see:**46```javascript47const items = reactive([])4849watch(items, (newItems) => {50console.log('Items count:', newItems.length)51})5253// Batch of changes54items.push('a') // length: 155items.push('b') // length: 256items.push('c') // length: 35758// Output: "Items count: 3"59// You won't see 1, 2, 3 logged separately60```6162**Using flush: 'sync' for immediate watching (use with caution):**63```javascript64import { ref, watch } from 'vue'6566const count = ref(0)6768// Sync watcher fires immediately on each change69watch(count, (newValue) => {70console.log('Immediate:', newValue)71}, { flush: 'sync' })7273count.value = 1 // Logs: "Immediate: 1"74count.value = 2 // Logs: "Immediate: 2"75count.value = 3 // Logs: "Immediate: 3"7677// WARNING: flush: 'sync' can cause performance issues78// and creates less predictable behavior. Avoid if possible.79```8081**Using nextTick to separate batches:**82```javascript83import { ref, watch, nextTick } from 'vue'8485const count = ref(0)8687watch(count, (newValue) => {88console.log('Count:', newValue)89})9091async function separatedUpdates() {92count.value = 193await nextTick() // Force flush94// Output: "Count: 1"9596count.value = 297await nextTick()98// Output: "Count: 2"99100count.value = 3101// Output: "Count: 3"102}103```104105**Practical example - form validation:**106```javascript107const formData = reactive({108email: '',109password: ''110})111112const validationErrors = ref([])113114// This watcher only fires once, with final form state115watch(formData, (data) => {116// Runs once after all fields are updated117validateForm(data)118}, { deep: true })119120// When user submits, you might update multiple fields121function populateFromSavedData(saved) {122formData.email = saved.email123formData.password = saved.password124// Validation runs once with both fields set125}126```127128**When batching helps performance:**129```javascript130// Without batching, this would trigger 1000 watcher/render cycles131const list = reactive([])132133function addManyItems() {134for (let i = 0; i < 1000; i++) {135list.push(i)136}137}138// With batching: renders once with all 1000 items139// Without batching: would render 1000 times!140```141142**Debugging intermediate states:**143```javascript144// If you need to observe every change for debugging:145import { ref, watch } from 'vue'146147const count = ref(0)148149// Method 1: Sync watcher (not recommended for production)150watch(count, (val) => console.log('DEBUG:', val), { flush: 'sync' })151152// Method 2: Track history manually153const history = []154const originalSet = count.value155Object.defineProperty(count, 'value', {156set(val) {157history.push(val)158originalSet.call(this, val)159}160})161```162163## Reference164- [Vue.js Reactivity in Depth](https://vuejs.org/guide/extras/reactivity-in-depth.html)165- [Vue.js Watchers - Callback Flush Timing](https://vuejs.org/guide/essentials/watchers.html#callback-flush-timing)166- [Vue.js nextTick()](https://vuejs.org/api/general.html#nexttick)167