Trung BìnhVue.js iconVue.js

`nextTick` là gì? Khi nào dùng?

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:

  1. Cần đọc DOM sau khi state update
  2. Focus element sau khi v-if toggle
  3. Scroll sau khi append item vào list

Xem toàn bộ Vue.js cùng filter theo level & chủ đề con.

Mở danh sách Vue.js