Sometimes Vue updates the page a little later after you change data. nextTick helps you wait for that update to finish before doing something else.
nextTick for DOM update timing in Vue
import { nextTick } from 'vue'; nextTick(() => { // code to run after DOM updates });
nextTick is a function you import from Vue.
You give it a function that runs after Vue finishes updating the page.
import { nextTick } from 'vue'; nextTick(() => { console.log('DOM updated'); });
import { ref, nextTick } from 'vue'; const show = ref(false); show.value = true; nextTick(() => { console.log('Now the element is visible'); });
import { ref, nextTick } from 'vue'; const visible = ref(false); const inputRef = ref(null); function showInput() { visible.value = true; nextTick(() => { inputRef.value?.focus(); }); }
This Vue component shows a button. When clicked, it shows an input box and then focuses it. The nextTick ensures the input exists before focusing.
<template>
<div>
<button @click="showInputBox">Show Input</button>
<input v-if="visible" ref="inputBox" placeholder="Type here" />
</div>
</template>
<script setup>
import { ref, nextTick } from 'vue';
const visible = ref(false);
const inputBox = ref(null);
function showInputBox() {
visible.value = true;
nextTick(() => {
inputBox.value?.focus();
});
}
</script>Without nextTick, trying to focus the input right after changing visible might fail because the input is not yet in the page.
nextTick returns a Promise if you want to use await instead of callbacks.
nextTick waits for Vue to finish updating the page before running code.
Use it when you need to work with the updated page elements right after data changes.
It helps avoid errors and makes your app feel smooth and responsive.
Practice
nextTick in Vue?Solution
Step 1: Understand Vue's update process
Vue batches DOM updates and applies them asynchronously after data changes.Step 2: Role of
nextTicknextTickschedules code to run after these DOM updates finish, ensuring the page is updated.Final Answer:
To run code after Vue updates the DOM -> Option BQuick Check:
nextTick waits for DOM update = A [OK]
- Thinking nextTick delays data changes
- Believing nextTick updates data immediately
- Assuming nextTick cancels updates
nextTick in Vue 3 Composition API?Solution
Step 1: Importing nextTick in Vue 3
In Vue 3 Composition API,nextTickis imported directly from 'vue'.Step 2: Using nextTick as a function
It is called asnextTick(() => { ... })to run code after DOM updates.Final Answer:
import { nextTick } from 'vue'; nextTick(() => { /* code */ }); -> Option AQuick Check:
Import and call nextTick correctly = A [OK]
- Using this.nextTick in Composition API
- Calling nextTick as a property of Vue
- Trying to use nextTick as a promise without calling
import { ref, nextTick } from 'vue';
const count = ref(0);
count.value = 1;
console.log(count.value);
nextTick(() => {
console.log(document.getElementById('count').textContent);
});
Assuming the template is <div id="count">{{ count }}</div>.Solution
Step 1: Understand reactive update timing
Settingcount.value = 1updates the reactive data immediately, soconsole.log(count.value)logs 1.Step 2: DOM update happens asynchronously
The DOM text content updates after Vue processes changes. UsingnextTickwaits for this update, so insidenextTickcallback, the DOM shows '1'.Final Answer:
Logs: 1 then '1' -> Option DQuick Check:
Data updates immediate, DOM updates after nextTick = D [OK]
- Expecting DOM to update before nextTick
- Confusing reactive data and DOM content timing
- Logging DOM content without nextTick
import { ref, nextTick } from 'vue';
const visible = ref(false);
visible.value = true;
console.log(document.getElementById('box').style.display);
nextTick(() => {
console.log(document.getElementById('box').style.display);
});
Template: <div id="box" v-if="visible" style="display:block">Box</div>Solution
Step 1: Timing of DOM updates with v-if
Settingvisible.value = truetriggers DOM update, but it happens asynchronously after current code.Step 2: First console.log runs before DOM update
Sodocument.getElementById('box')is null or style.display is empty at first log.Step 3: nextTick callback runs after DOM update
InsidenextTick, the element exists and style.display is 'block'.Final Answer:
The first console.log runs before DOM updates, so it logs empty string -> Option AQuick Check:
DOM updates delayed, nextTick waits = C [OK]
- Expecting DOM to update immediately after reactive change
- Misusing nextTick causing runtime errors
- Confusing ref with reactive for simple boolean
showInput changes from false to true. Which is the best way to ensure the input is focused correctly?Solution
Step 1: Understand DOM update timing with reactive changes
WhenshowInputchanges, Vue updates the DOM asynchronously, so the input may not exist immediately.Step 2: Use nextTick to wait for DOM update
CallingnextTick(() => inputElement.focus())ensures the input is in the DOM before focusing.Step 3: Other options are less reliable
Calling focus immediately may fail; setTimeout is less precise; autofocus attribute may not work if input is conditionally rendered.Final Answer:
Use nextTick(() => inputElement.focus()) after setting showInput = true -> Option CQuick Check:
Wait DOM update with nextTick before focus = B [OK]
- Focusing input before it exists in DOM
- Relying on autofocus attribute for conditional elements
- Using setTimeout instead of nextTick
Ready to prove it?
Pass the test and this pattern moves unknown
