Vue's nextTick
is a core function that helps manage the timing of DOM updates in Vue applications. It provides a way to execute code after Vue has finished updating the DOM, ensuring you're working with the latest rendered state.
Vue performs DOM updates asynchronously. When you change reactive data, Vue doesn't immediately update the DOM. Instead, it:
This optimization prevents unnecessary DOM updates and improves performance.
You need nextTick
when:
clientWidth
and clientHeight
)Let’s take a look at a practical example of when nextTick
might be helpful. In the code below, we're adding items to a scrollable list. Without nextTick
, attempting to scroll to the bottom immediately after adding an item wouldn't work but using nextTick
ensures we access the correct scrollHeight
after the DOM has been updated.
<script setup>
import { ref, nextTick } from 'vue';
const items = ref([]);
const newItem = ref('');
const itemsList = ref(null);
const addItem = async () => {
// Add item to list
items.value.push(newItem.value);
newItem.value = '';
// This won't work immediately
console.log('before DOM Update', itemsList.value.scrollHeight);
// Wait for DOM update
await nextTick();
// Now we can safely access the updated DOM
const container = itemsList.value;
container.scrollTop = container.scrollHeight;
console.log('after DOM Update', itemsList.value.scrollHeight);
};
</script>
<template>
<div>
<div ref="itemsList" class="items-container">
<div v-for="item in items" :key="item">
{{ item }}
</div>
</div>
<input v-model="newItem" @keyup.enter="addItem" />
<button @click="addItem">Add Item</button>
</div>
</template>
<style scoped>
.items-container {
height: 50px;
margin-bottom: 5px;
border: 1px solid black;
padding: 5px;
overflow: scroll;
}
</style>
See the example in action in this StackBlitz playground. Try removing the nextTick to see what happens.
nextTick
must be imported from Vue and can be used in a couple different ways. Since it returns a promise you can await
the promise resolution. You can use .then
to call a function when the promise resolves. Finally, you could pass it a callback function as the only argument.
// Using nextTick imported from vue
import { nextTick } from 'vue'
// Promise syntax
await nextTick()
// DOM is now updated
// Alt promise syntax
nextTick().then(()=>{
// DOM is now updated
})
// Callback syntax
nextTick(() => {
// DOM is now updated
})
While extremely handy, there are a couple common mistakes people make when using nextTick
just like some other tools Vue provides.
nextTick
doesn't guarantee third-party DOM changes are complete, only Vue's updates, so consult the thrid-party’s docs for info on any events they provideWhile not a tool you reach for every day, understanding nextTick
can help elevate your Vue development skills and open up more possibilities for good UX.
Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.
More than 200.000 users have already joined us. You are welcome too!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.