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?
AonCreated
BonUnmounted
ConUpdated
DonMounted
✗ 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?
AInside setup() function
BOutside any function
CIn the template section
DIn a global script
✗ 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?
ATemplate markup
BStatic HTML content
CCSS styles
DTimers like setTimeout or setInterval
✗ Incorrect
Timers continue running unless cleared, so they need cleanup.
What problem can happen if cleanup is not done properly?
AMemory leaks and slow app performance
BFaster app loading
CBetter user experience
DAutomatic bug fixes
✗ Incorrect
Not cleaning up causes memory leaks, which slow down the app.
Which of these is NOT a good practice for cleanup in Vue?
AUsing onUnmounted inside setup()
BClearing timers when component unmounts
CLeaving event listeners active after component removal
DRemoving subscriptions on unmount
✗ 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.
Practice
(1/5)
1. In Vue, which lifecycle hook is best to use for cleaning up timers or event listeners when a component is removed?
easy
A. onUpdated
B. onMounted
C. onUnmounted
D. onCreated
Solution
Step 1: Understand Vue lifecycle hooks
onMounted runs when the component appears, onUnmounted runs when it disappears.
Step 2: Identify cleanup timing
Cleanup like stopping timers or removing listeners should happen when the component is removed, so onUnmounted is the right hook.
Final Answer:
onUnmounted -> Option C
Quick Check:
Cleanup runs on unmount = C [OK]
Hint: Cleanup always goes in onUnmounted hook [OK]
Common Mistakes:
Using onMounted for cleanup
Confusing onUpdated with cleanup
Trying to cleanup in onCreated
2. Which of the following is the correct syntax to register a cleanup function in Vue's setup() using the onUnmounted hook?
easy
A. onUnmounted(() => { clearInterval(timer) })
B. onUnmounted = () => { clearInterval(timer) }
C. onUnmounted.clear(() => { clearInterval(timer) })
D. onUnmounted.clearInterval(timer)
Solution
Step 1: Recall Vue hook syntax
Vue lifecycle hooks like onUnmounted are functions that take a callback to run on that event.
Step 2: Match correct syntax
The correct way is calling onUnmounted with a function: onUnmounted(() => { ... }). Other options misuse assignment or methods.
Final Answer:
onUnmounted(() => { clearInterval(timer) }) -> Option A
Quick Check:
Hook usage = function call with callback [OK]
Hint: Hooks are called with a function, not assigned [OK]
Common Mistakes:
Assigning instead of calling onUnmounted
Using nonexistent methods on onUnmounted
Missing arrow function wrapper
3. What will be the console output when this Vue component unmounts?
A. Cleanup runs too early; should use onUnmounted instead
B. Timer is never created; setInterval is missing
C. clearInterval is called without timer variable
D. No problem; code works correctly
Solution
Step 1: Check when cleanup runs
Using onMounted to clear interval means it stops immediately after mounting, so no ticks happen.
Step 2: Correct cleanup timing
Cleanup should happen when component unmounts, so onUnmounted is the correct hook to clear the timer.
Final Answer:
Cleanup runs too early; should use onUnmounted instead -> Option A
Quick Check:
Cleanup must be on unmount, not mount [OK]
Hint: Cleanup belongs in onUnmounted, not onMounted [OK]
Common Mistakes:
Clearing timer on mount instead of unmount
Assuming onMounted is for cleanup
Ignoring lifecycle hook purpose
5. You have a Vue component that subscribes to a WebSocket in setup(). Which pattern correctly ensures the subscription is cleaned up only when the component unmounts?
setup() {
const socket = new WebSocket('wss://example.com')
socket.onmessage = (event) => console.log(event.data)
// What cleanup code and where?
}
hard
A. Call socket.close() immediately after creating socket
B. No cleanup needed for WebSocket in Vue
C. Use onMounted(() => socket.close()) to close socket on mount
D. Use onUnmounted(() => socket.close()) inside setup
Solution
Step 1: Understand WebSocket lifecycle
WebSocket stays open until closed; closing it too early stops messages.
Step 2: Place cleanup in onUnmounted
To avoid leaks, close the socket when component unmounts using onUnmounted(() => socket.close()).
Final Answer:
Use onUnmounted(() => socket.close()) inside setup -> Option D
Quick Check:
Cleanup subscriptions on unmount with onUnmounted [OK]
Hint: Close subscriptions in onUnmounted hook [OK]
Common Mistakes:
Closing socket immediately after creation
Closing socket on mount instead of unmount
Skipping cleanup causing memory leaks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.