Vue batch DOM updates — không update ngay khi state thay đổi, mà queue updates và flush async. nextTick cho phép chờ DOM được update xong:
javascript
import { nextTick, ref } from 'vue'
const count = ref(0)
async function handleClick() {
count.value++
// DOM chưa update ở đây
await nextTick()
// DOM đã update — giờ có thể đọc DOM hoặc scroll
console.log(document.querySelector('.count')?.textContent)
}Dùng khi:
- Cần đọc DOM sau khi state update
- Focus element sau khi v-if toggle
- Scroll sau khi append item vào list