Wait for the DOM with nextTick
When you change reactive state, Vue does not update the DOM immediately. It batches the change and flushes it on the next tick, so reading the DOM right after a change still shows the old value:
count.value += 1;
// the DOM still shows the old count here
nextTick returns a promise that resolves after Vue has flushed those updates:
<script setup>
import { ref, nextTick } from 'vue';
const count = ref(0);
const label = ref(null);
const snapshot = ref('');
async function increment() {
count.value += 1;
await nextTick();
snapshot.value = label.value.textContent;
}
</script>
<template>
<button ref="label" @click="increment">Count: {{ count }}</button>
<p>{{ snapshot }}</p>
</template>
The button carries a template ref, label, which points at the element. After
await nextTick(), label.value.textContent reads the updated text, so the
paragraph shows what the button now says. Without the await, it would capture
the old text.
Use nextTick when you must read or measure the DOM after a change — focusing an
input, scrolling to an element, or reading a size.
The Test for this Lesson mounts your component, clicks the button once, and
checks the snapshot. Your component is exported from App.vue.
Challenge
The snapshot shows the button's old text. Wait for the DOM before reading it:
export an App component from App.vue that renders a Count button and a
paragraph showing the button's text after each click.
The Test mounts your component, clicks the button once, and expects the paragraph
to read Count: 1. The starter reads the button before the DOM updates, so it
captures Count: 0 and the Test fails.