Challenge - 5 Problems
Vue Lifecycle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Understanding onBeforeUpdate timing
In a Vue 3 component using the Composition API, when does the
Choose the correct description of its timing relative to the DOM update.
onBeforeUpdate lifecycle hook run?Choose the correct description of its timing relative to the DOM update.
Attempts:
2 left
💡 Hint
Think about when Vue needs to prepare the DOM before showing changes.
✗ Incorrect
The onBeforeUpdate hook runs after reactive data changes but before the DOM updates. This lets you perform actions or checks before the DOM reflects the new state.
❓ component_behavior
intermediate2:00remaining
Effect of onUpdated hook on DOM
What happens when the
Choose the correct statement about its effect on the DOM.
onUpdated lifecycle hook runs in a Vue 3 component?Choose the correct statement about its effect on the DOM.
Attempts:
2 left
💡 Hint
Consider when you want to read or manipulate the updated DOM.
✗ Incorrect
The onUpdated hook runs after the DOM updates, so you can safely access or manipulate the new DOM state.
❓ state_output
advanced2:00remaining
Output order of onBeforeUpdate and onUpdated
Given this Vue 3 component code, what is the order of console logs when the button is clicked?
When the button triggers
import { ref, onBeforeUpdate, onUpdated } from 'vue';
export default {
setup() {
const count = ref(0);
onBeforeUpdate(() => console.log('Before Update'));
onUpdated(() => console.log('Updated'));
function increment() {
count.value++;
}
return { count, increment };
}
}When the button triggers
increment(), what logs appear and in what order?Attempts:
2 left
💡 Hint
Remember the lifecycle hooks run around the DOM update.
✗ Incorrect
When reactive data changes, onBeforeUpdate runs first before the DOM updates, then onUpdated runs after the DOM updates.
🔧 Debug
advanced2:00remaining
Why does onUpdated not run?
A developer writes this Vue 3 component:
But when
import { ref, onUpdated } from 'vue';
export default {
setup() {
const message = ref('Hello');
onUpdated(() => console.log('Component updated'));
function changeMessage() {
message.value = 'Hi';
}
return { message, changeMessage };
}
}But when
changeMessage() is called, the console log never appears. What is the most likely reason?Attempts:
2 left
💡 Hint
Think about when Vue triggers DOM updates.
✗ Incorrect
If the reactive data changes but is not used in the template, Vue does not update the DOM, so onUpdated does not run.
🧠 Conceptual
expert2:00remaining
Distinguishing onBeforeUpdate and onUpdated use cases
Which scenario best fits using
onBeforeUpdate instead of onUpdated in a Vue 3 component?Attempts:
2 left
💡 Hint
Think about when you need to capture the old state before changes.
✗ Incorrect
onBeforeUpdate runs before the DOM changes, so it's ideal to capture the current DOM state before updates. onUpdated runs after the DOM changes.