Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/dom-update-timing-nexttick.md
1---2title: Use nextTick() to Wait for DOM Updates3impact: MEDIUM4impactDescription: DOM updates are batched and asynchronous - direct DOM access after state changes sees stale values5type: capability6tags: [vue3, dom, nextTick, reactivity, async]7---89# Use nextTick() to Wait for DOM Updates1011**Impact: MEDIUM** - Vue batches DOM updates asynchronously for performance. If you access the DOM immediately after changing reactive state, you'll see the old values. Use `nextTick()` to wait for the DOM to update.1213When you modify reactive state, Vue doesn't update the DOM synchronously. Instead, it buffers changes and applies them in the next "tick" of the event loop. This is a performance optimization, but it can cause bugs when you need to read from or manipulate the DOM after state changes.1415## Task Checklist1617- [ ] Use `await nextTick()` when you need to access updated DOM elements after state changes18- [ ] Use `nextTick()` when measuring DOM elements (heights, widths) after data changes19- [ ] Use `nextTick()` when focusing inputs or scrolling after content updates20- [ ] Consider if you really need DOM access - often you can work with reactive data instead2122**Incorrect:**23```javascript24import { ref } from 'vue'2526const message = ref('Hello')27const messageEl = ref(null)2829function updateMessage() {30message.value = 'Updated!'3132// WRONG: DOM still shows "Hello" at this point33console.log(messageEl.value.textContent) // "Hello" - stale!3435// WRONG: Scrolling/focusing may not work correctly36scrollContainer.value.scrollTop = scrollContainer.value.scrollHeight37}38```3940**Correct:**41```javascript42import { ref, nextTick } from 'vue'4344const message = ref('Hello')45const messageEl = ref(null)4647async function updateMessage() {48message.value = 'Updated!'4950// CORRECT: Wait for DOM to update51await nextTick()5253// Now the DOM is updated54console.log(messageEl.value.textContent) // "Updated!"5556// Scrolling and focusing now work correctly57scrollContainer.value.scrollTop = scrollContainer.value.scrollHeight58}5960// Alternative: callback syntax61function updateWithCallback() {62message.value = 'Updated!'6364nextTick(() => {65console.log(messageEl.value.textContent) // "Updated!"66})67}68```6970```vue71<script setup>72import { ref, nextTick } from 'vue'7374const items = ref([])75const listRef = ref(null)7677async function addItem() {78items.value.push({ id: Date.now(), text: 'New item' })7980await nextTick()8182// Now we can safely scroll to the new item83listRef.value.lastElementChild?.scrollIntoView({ behavior: 'smooth' })84}85</script>86```8788## Reference89- [Vue.js Reactivity Fundamentals - DOM Update Timing](https://vuejs.org/guide/essentials/reactivity-fundamentals.html#dom-update-timing)90- [Vue.js nextTick API](https://vuejs.org/api/general.html#nexttick)91