Challenge - 5 Problems
Vue Composition API Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when the
onMounted hook runs?Consider this Vue 3 component using Composition API:
What will the component display after it is mounted?
import { ref, onMounted } from 'vue';
export default {
setup() {
const message = ref('');
onMounted(() => {
message.value = 'Hello from mounted!';
});
return { message };
}
}What will the component display after it is mounted?
Attempts:
2 left
💡 Hint
Remember that
onMounted runs after the component is inserted into the DOM.✗ Incorrect
The onMounted hook runs after the component is mounted. It updates the reactive message value, causing the component to re-render and show the new message.
❓ state_output
intermediate2:00remaining
What is the value of
count after onBeforeUpdate runs?Look at this Vue 3 setup function:
If the user clicks a button that calls
import { ref, onBeforeUpdate } from 'vue';
export default {
setup() {
const count = ref(0);
onBeforeUpdate(() => {
count.value += 1;
});
function increment() {
count.value += 1;
}
return { count, increment };
}
}If the user clicks a button that calls
increment once, what will be the value of count after the onBeforeUpdate hook runs?Attempts:
2 left
💡 Hint
The
onBeforeUpdate hook runs before the DOM updates but after reactive state changes.✗ Incorrect
When increment is called, count increases from 0 to 1. Then onBeforeUpdate runs and adds 1 more, making count equal to 2.
📝 Syntax
advanced2:00remaining
Which option correctly uses
onUnmounted in Composition API?Select the code snippet that correctly registers a cleanup function to run when the component unmounts.
Attempts:
2 left
💡 Hint
Check the exact function name and syntax for arrow functions.
✗ Incorrect
The correct hook name is onUnmounted and it requires a function argument. Option A uses correct syntax with an arrow function and parentheses.
🔧 Debug
advanced2:00remaining
Why does this
onUpdated hook cause an infinite loop?Examine this setup function:
What causes the infinite update loop?
import { ref, onUpdated } from 'vue';
export default {
setup() {
const count = ref(0);
onUpdated(() => {
count.value++;
});
return { count };
}
}What causes the infinite update loop?
Attempts:
2 left
💡 Hint
Think about what happens when reactive state changes inside update hooks.
✗ Incorrect
Modifying reactive state inside onUpdated triggers another update, causing an endless loop of updates.
🧠 Conceptual
expert2:00remaining
Which lifecycle hook runs only once during the component's lifetime?
In Vue 3 Composition API, which lifecycle hook is guaranteed to run exactly once per component instance?
Attempts:
2 left
💡 Hint
Think about hooks that run multiple times versus those that run once.
✗ Incorrect
onMounted runs once after the component is inserted into the DOM. Other hooks like onUpdated and onBeforeUpdate can run many times. onUnmounted runs once but only when the component is removed.