Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 87 ยท Reactivity Fundamentals

DOM Update Timing and nextTick

Understand why Vue updates the DOM asynchronously, how changes are batched into one render, when to use nextTick, and how watcher flush timing decides what the DOM looks like.

Here is a puzzle that trips up almost every Vue beginner. You change a ref, then immediately read the page, and the page still shows the old value. Nothing is broken. Vue updates the DOM asynchronously, and the state you changed and the DOM that shows it are briefly out of step.

Understanding this small gap explains batching (why ten changes cost one render), tells you when you need nextTick, and clarifies the difference between watchers that run before the render and watchers that run after it. If you need a refresher on watchers, see Watchers; for how a write notifies the renderer, see How Reactivity Works.

State is instant, the DOM is not

When you assign to a ref, the new value is available immediately from JavaScript: count.value is the new number on the very next line. What Vue does not do is patch the DOM on that line. Instead it notes that the component is out of date and schedules one update job on the microtask queue, which runs after your current synchronous code finishes.

JavaScript
count.value = 5;
console.log(count.value);                     // 5, state is new
console.log(label.value.textContent);         // still the old text
await nextTick();
console.log(label.value.textContent);         // "5"

nextTick() returns a promise that resolves after Vue has finished the pending DOM update. You can await it, or pass a callback: nextTick(() => { ... }). In the Options API it is this.$nextTick().

flowchart TD
    A["Your code changes state, many times"] --> B["Vue queues ONE update job per component"]
    B --> C["Your synchronous code finishes"]
    C --> D["pre watchers run"]
    D --> E["Components re-render, DOM is patched"]
    E --> F["post watchers and onUpdated run"]
    F --> G["nextTick promises resolve"]

Changes are collected while your code runs; the flush happens afterwards, and nextTick waits for the end of it.

Batching in action

Because the render is deferred, Vue can batch changes. If you set five different refs in one click handler, the component renders once, with all five new values. If you set the same ref three times, only the last value is ever drawn. There are no half-updated screens, and no wasted work.

The demo below changes a counter three times in one handler, then reads the DOM right away, then again after await nextTick(). It also counts how many renders happened.

Reading the DOM before and after nextTick

On the first press the list reads: state is 3, the DOM still shows 0, after nextTick it shows 3, and exactly one render was caused by the three changes. The browser never painted a 1 or a 2.

One consequence is worth remembering: do not rely on the DOM reflecting state inside the same synchronous block. If you need the updated DOM, wait for it.

When you actually need nextTick

Most code never calls nextTick, because templates describe the DOM declaratively. You need it when you must touch the real DOM right after changing state that affects it. Typical cases:

  • Focus a field that appears with v-if. The element does not exist until the render has happened.
  • Scroll to the bottom after adding a message to a chat list. The new row must be in the DOM before you can measure it.
  • Measure an element (width, height, position) after content changed.
  • Hand a freshly rendered element to a third-party library.

Each of them has the same shape: change the state, await nextTick(), then use the DOM.

Focus an input that does not exist yet

Before the wait, the template ref is still null; after it, the input exists and focus() works. (The input uses v-model, which is covered in Two-Way Binding with v-model. Here it only keeps the field editable.)

Watcher flush timing and nextTick

Watchers are part of the same flush. By default (flush: "pre"), a watcher callback runs before the component re-renders, so reading the DOM inside it gives you the old DOM. If the callback needs the updated DOM, there are two options: await nextTick() inside the callback, or use flush: "post", which runs the callback after the DOM has been patched. There is a shorthand for effects: watchPostEffect(fn).

Pre and post watchers see different DOMs

After one press, the pre watcher reports state 2 with the DOM still showing 1, and the post watcher reports state 2 with the DOM showing 2. In a real browser the post watcher is where you would measure layout, because sizes and positions are only correct once the DOM has been patched.

A few things to remember

  • Writing state inside a post watcher or onUpdated can trigger another update. That is allowed, but guard it, or you create a loop.
  • flush: "sync" makes a watcher run on every change immediately and opts out of batching. It is for rare cases such as debugging.
  • nextTick waits for the next flush. If nothing is pending, it simply resolves on the next microtask.
  • Reading a computed property right after changing its source is always correct: computeds are evaluated on demand, not on the DOM schedule.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You write count.value = 5 and then read count.value on the next line. What do you get?

    Reactive state is plain JavaScript data and changes immediately. Vue only defers the DOM update to a later microtask.
  2. 2. A click handler sets three different refs that a component displays. How many times does the component render?

    Vue schedules one update job per component and runs it after your synchronous code finishes, so all three changes appear together.
  3. 3. What is the correct way to focus an input that is shown with v-if after you set editing.value = true?

    Until the update flushes, the element does not exist and the template ref is still null. nextTick resolves after the DOM is patched.
  4. 4. Which watcher option makes a callback see the DOM after Vue has updated it?

    flush: 'post' defers the callback until the component's DOM has been updated. The default 'pre' runs before the render and sees the old DOM.

Summary

  • State changes are immediate; DOM updates are scheduled and run after your synchronous code ends.
  • Many changes in the same tick cost one render per component, and only the final values are drawn.
  • await nextTick() resolves after the pending DOM update, so you can read or touch the fresh DOM.
  • Use it for focusing, scrolling, measuring and integrating with libraries, not for ordinary rendering.
  • Default watchers run before the render; flush: "post" (or watchPostEffect) runs after it.
  • Avoid unguarded state writes in post-flush code, which can cause update loops.

Next, collect the traps that cost developers the most time in Reactivity Pitfalls.