0
0
Vueframework~20 mins

Lifecycle hooks in Composition API in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Composition API Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the onMounted hook runs?
Consider this Vue 3 component using Composition API:
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?
AThe component initially shows an empty string, then updates to 'Hello from mounted!' after mounting.
BThe component shows 'Hello from mounted!' immediately before mounting.
CThe component never updates the message and stays empty.
DThe component throws an error because <code>onMounted</code> cannot access <code>message</code>.
Attempts:
2 left
💡 Hint
Remember that onMounted runs after the component is inserted into the DOM.
state_output
intermediate
2:00remaining
What is the value of count after onBeforeUpdate runs?
Look at this Vue 3 setup function:
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?
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
The onBeforeUpdate hook runs before the DOM updates but after reactive state changes.
📝 Syntax
advanced
2: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.
AonUnmounted(() => { console.log('Component unmounted'); });
BonUnmounted { console.log('Component unmounted'); }
ConUnmount(() => { console.log('Component unmounted'); });
DonUnmounted(() => console.log('Component unmounted'))
Attempts:
2 left
💡 Hint
Check the exact function name and syntax for arrow functions.
🔧 Debug
advanced
2:00remaining
Why does this onUpdated hook cause an infinite loop?
Examine this setup function:
import { ref, onUpdated } from 'vue';

export default {
  setup() {
    const count = ref(0);
    onUpdated(() => {
      count.value++;
    });
    return { count };
  }
}

What causes the infinite update loop?
AThe component lacks a template, so updates never finish.
BThe <code>count</code> ref is not initialized properly.
CThe <code>onUpdated</code> hook is missing a cleanup function.
DThe <code>onUpdated</code> hook changes reactive state, triggering another update endlessly.
Attempts:
2 left
💡 Hint
Think about what happens when reactive state changes inside update hooks.
🧠 Conceptual
expert
2: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?
AonUnmounted
BonMounted
ConBeforeUpdate
DonUpdated
Attempts:
2 left
💡 Hint
Think about hooks that run multiple times versus those that run once.