What is the purpose of lifecycle hooks in Vue's Composition API?
Lifecycle hooks let you run code at specific times during a component's life, like when it starts, updates, or stops. They help manage side effects and resource cleanup.
Click to reveal answer
beginner
Name three common lifecycle hooks used in Vue's Composition API.
Three common hooks are onMounted (runs after the component is added to the page), onUpdated (runs after reactive data changes update the DOM), and onUnmounted (runs after the component is removed from the DOM).
Click to reveal answer
beginner
How do you use onMounted in the Composition API?
Import <code>onMounted</code> from Vue, then call it inside the <code>setup()</code> function with a callback. This callback runs after the component appears on the page.
Click to reveal answer
intermediate
What is the difference between onBeforeUnmount and onUnmounted?
onBeforeUnmount runs just before the component is removed, letting you clean up resources early. onUnmounted runs right after the component is removed from the DOM.
Click to reveal answer
intermediate
Why is it better to use lifecycle hooks inside setup() in Composition API instead of Options API?
Using hooks inside setup() groups related logic together, making code easier to read and maintain. It avoids scattering lifecycle code across different options like mounted or updated.
Click to reveal answer
Which lifecycle hook runs after the component is added to the DOM in Vue's Composition API?
AonMounted
BonUpdated
ConUnmounted
DonBeforeMount
✗ Incorrect
onMounted runs after the component is inserted into the DOM, perfect for starting side effects.
Where do you place lifecycle hooks when using Vue's Composition API?
AOutside the component definition
BInside the <code>setup()</code> function
CInside the <code>methods</code> option
DInside the <code>data()</code> option
✗ Incorrect
Lifecycle hooks are called inside setup() to keep logic organized.
Which hook would you use to clean up timers or event listeners before a component is removed?
AonMounted
BonUpdated
ConBeforeUnmount
DonCreated
✗ Incorrect
onBeforeUnmount is used to clean up resources before the component disappears.
What does the onUpdated hook do in Vue's Composition API?
ARuns only once when the app starts
BRuns before the component is mounted
CRuns when the component is destroyed
DRuns after reactive data changes update the DOM
✗ Incorrect
onUpdated runs after the DOM updates due to reactive data changes.
Which import is needed to use lifecycle hooks in Vue's Composition API?
Aimport { onMounted } from 'vue'
Bimport { useEffect } from 'vue'
Cimport { lifecycle } from 'vue'
Dimport { componentDidMount } from 'vue'
✗ Incorrect
Vue provides lifecycle hooks like onMounted directly from the 'vue' package.
Explain how to use lifecycle hooks in Vue's Composition API and why they are useful.
Think about when you want to run code during a component's life.
You got /4 concepts.
Describe the difference between onBeforeUnmount and onUnmounted hooks in Vue's Composition API.
Consider when you want to stop timers or listeners.
You got /3 concepts.
Practice
(1/5)
1. Which lifecycle hook in Vue's Composition API runs code right after the component is added to the page?
easy
A. onUpdated
B. onCreated
C. onDestroyed
D. onMounted
Solution
Step 1: Understand the lifecycle phases
The onMounted hook runs after the component is inserted into the DOM.
Step 2: Match the hook to the action
Since the question asks for the hook that runs right after adding to the page, onMounted is the correct choice.
Final Answer:
onMounted -> Option D
Quick Check:
Mounted = after insert [OK]
Hint: Remember: mounted means component is on the page [OK]
Common Mistakes:
Confusing onMounted with onCreated
Thinking onUpdated runs after mounting
Using onDestroyed for setup tasks
2. Which of these is the correct way to import and use the onUnmounted hook inside the setup() function?
easy
A. import onUnmounted from 'vue'; setup() { onUnmounted(() => { /* cleanup */ }); }
B. import { onUnmounted } from 'vue'; setup() { onUnmounted(() => { /* cleanup */ }); }
C. import { onUnmounted } from 'vue'; setup() { onUnmounted = () => { /* cleanup */ }; }
D. import { onUnmounted } from 'vue'; setup() { onUnmounted; }
Solution
Step 1: Check import syntax
The correct import uses curly braces: import { onUnmounted } from 'vue';
Step 2: Check usage inside setup
The hook is called as a function with a callback: onUnmounted(() => { /* cleanup */ });
Final Answer:
Correct import and call syntax -> Option B
Quick Check:
Import with braces + call hook [OK]
Hint: Import hooks with braces and call them as functions [OK]
Common Mistakes:
Importing without braces
Assigning hook to a variable instead of calling
Calling hook without a callback function
3. What will be logged to the console when this Vue component runs?
A. onMounted is incorrectly assigned instead of called
B. Missing import of onUnmounted
C. setup() should return a value
D. Console.log syntax is wrong
Solution
Step 1: Check how onMounted is used
The code assigns a function to onMounted instead of calling it with a callback.
Step 2: Understand correct usage
The correct usage is onMounted(() => { ... }), not assignment.
Final Answer:
onMounted is incorrectly assigned instead of called -> Option A
Quick Check:
Hooks are called, not reassigned [OK]
Hint: Use onMounted(callback), don't assign it like a variable [OK]
Common Mistakes:
Assigning hook instead of calling it
Forgetting to pass a function to onMounted
Thinking setup must always return something
5. You want to fetch data when a component mounts and cancel a timer when it unmounts using Composition API. Which code snippet correctly uses lifecycle hooks to do this?
hard
A.
import { ref, onUnmounted } from 'vue';
export default {
setup() {
const data = ref(null);
let timer = setInterval(() => console.log('tick'), 1000);
onUnmounted(() => {
clearInterval(timer);
});
return { data };
}
}