Recall & Review
beginner
What is the purpose of cleanup in Vue's lifecycle?
Cleanup helps remove side effects like timers, event listeners, or subscriptions when a component is no longer needed. This prevents memory leaks and keeps the app efficient.
Click to reveal answer
beginner
Where should you place cleanup code in Vue 3 Composition API?
Use the
onUnmounted lifecycle hook inside the setup() function to run cleanup code when the component is destroyed.Click to reveal answer
intermediate
Why is it important to keep cleanup code scoped inside
setup()?Keeping cleanup code inside
setup() ensures it only affects the current component instance and avoids accidental interference with other parts of the app.Click to reveal answer
beginner
How do you clean up a timer in Vue 3 Composition API?
Create the timer inside
setup(), store its ID, and clear it inside onUnmounted(() => clearTimeout(timerId)) to stop it when the component unmounts.Click to reveal answer
beginner
What happens if you forget to clean up side effects in Vue components?
Side effects like timers or event listeners keep running, causing memory leaks and unexpected behavior, which can slow down or break your app over time.
Click to reveal answer
In Vue 3 Composition API, which lifecycle hook is used for cleanup?
✗ Incorrect
The
onUnmounted hook runs when the component is removed, making it the right place for cleanup.Where should cleanup code be placed to keep it effective and scoped?
✗ Incorrect
Placing cleanup inside
setup() keeps it tied to the component's lifecycle and scope.What is a common side effect that needs cleanup in Vue components?
✗ Incorrect
Timers continue running unless cleared, so they need cleanup.
What problem can happen if cleanup is not done properly?
✗ Incorrect
Not cleaning up causes memory leaks, which slow down the app.
Which of these is NOT a good practice for cleanup in Vue?
✗ Incorrect
Leaving event listeners active causes memory leaks and bugs.
Explain how to properly scope cleanup code in a Vue 3 component using the Composition API.
Think about where side effects start and where they should end.
You got /4 concepts.
Describe why cleanup is important in Vue components and what can happen if it is neglected.
Consider what happens when components disappear but their effects keep running.
You got /4 concepts.