0
0
Vueframework~20 mins

nextTick for DOM update timing in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue nextTick Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the output after calling nextTick?

Consider this Vue 3 component using nextTick. What will be logged to the console?

Vue
import { ref, nextTick } from 'vue';

export default {
  setup() {
    const count = ref(0);

    function increment() {
      count.value++;
      console.log('Before nextTick:', count.value);
      nextTick(() => {
        console.log('Inside nextTick:', count.value);
      });
    }

    return { count, increment };
  }
};
ABefore nextTick: 1\nInside nextTick: 1
BBefore nextTick: 0\nInside nextTick: 1
CBefore nextTick: 1\nInside nextTick: 0
DBefore nextTick: 0\nInside nextTick: 0
Attempts:
2 left
💡 Hint

Remember that count.value updates immediately, but DOM updates happen after nextTick.

state_output
intermediate
2:00remaining
What is the DOM content after nextTick callback?

Given this Vue 3 component, what will be the content of the <div> after the nextTick callback runs?

Vue
<template>
  <div>{{ message }}</div>
  <button @click="updateMessage">Update</button>
</template>

<script setup>
import { ref, nextTick } from 'vue';

const message = ref('Hello');

function updateMessage() {
  message.value = 'Updated';
  nextTick(() => {
    const div = document.querySelector('div');
    console.log(div.textContent);
  });
}
</script>
AHello
BUpdated
Cundefined
DError: div is null
Attempts:
2 left
💡 Hint

Think about when the DOM updates happen relative to nextTick.

🔧 Debug
advanced
2:00remaining
Why does this nextTick callback not see updated DOM?

In this Vue 3 setup, the nextTick callback logs the old DOM content instead of the updated one. What is the cause?

Vue
<template>
  <p ref="para">Old</p>
  <button @click="changeText">Change</button>
</template>

<script setup>
import { ref, nextTick } from 'vue';

const para = ref(null);

function changeText() {
  para.value.textContent = 'New';
  nextTick(() => {
    console.log(para.value.textContent);
  });
}
</script>
ADirect DOM change bypasses Vue reactivity, so nextTick does not wait for it
BnextTick callback runs before the DOM update
Cpara ref is null inside nextTick callback
DVue automatically reverts manual DOM changes after nextTick
Attempts:
2 left
💡 Hint

Consider how Vue tracks changes and when nextTick triggers.

📝 Syntax
advanced
2:00remaining
Which nextTick usage is syntactically correct in Vue 3 Composition API?

Choose the correct way to use nextTick to run code after DOM updates.

AnextTick(console.log('DOM updated'));
BnextTick.then(() => console.log('DOM updated'));
CnextTick(() => { console.log('DOM updated'); });
Dawait nextTick(); console.log('DOM updated');
Attempts:
2 left
💡 Hint

Remember nextTick returns a Promise and can be awaited or passed a callback function.

🧠 Conceptual
expert
2:00remaining
Why use nextTick in Vue 3 when updating reactive data?

Which statement best explains the purpose of nextTick in Vue 3?

A<p>It batches multiple reactive data changes into a single update cycle.</p>
B<p>It prevents Vue from updating the DOM automatically.</p>
C<p>It immediately forces synchronous DOM updates before continuing.</p>
D<p>It delays code execution until after the DOM has updated to reflect reactive data changes.</p>
Attempts:
2 left
💡 Hint

Think about when DOM updates happen relative to reactive data changes.