CHAPTER 13
Beginner
Vue Lifecycle Hooks
Updated: May 18, 2026
5 min read
# CHAPTER 13
Vue Lifecycle Hooks
1. Chapter Introduction
Every Vue component goes through a lifecycle: it is created, mounted to the DOM, may update when data changes, and eventually unmounted. Lifecycle hooks let you run code at specific phases — fetching data when mounted, cleaning up timers before unmounting.2. Learning Objectives
- Understand the complete Vue 3 component lifecycle.
-
Use
onMountedfor DOM operations and data fetching.
-
Use
onUpdatedfor post-update DOM reads.
-
Use
onUnmountedfor cleanup.
-
Use
onBeforeMountand other pre-hooks.
3. Lifecycle Flow Diagram
text
4. Lifecycle Hooks Code
vue
5. Practical Patterns
vue
6. Common Mistakes
-
Fetching data in
setup()directly (synchronously): Data fetching is async. Always useonMountedwithasync/awaitfor initial data fetches.
-
Mutating state in
onUpdated: This creates an infinite loop — update → onUpdated → update → onUpdated. Read DOM inonUpdated, don't write state.
7. MCQs
Question 1
Best lifecycle hook for API calls on load?
Question 2
onUnmounted is used for?
Question 3
onUpdated fires when?
Question 4
Is the DOM accessible in setup()?
Question 5
onBeforeUnmount vs onUnmounted?
Question 6
onActivated is for?
Question 7
onErrorCaptured catches errors from?
Question 8
What does return false in onErrorCaptured do?
Question 9
Template refs are accessible in?
Question 10
Infinite loop risk exists in?
8. Interview Questions
-
Q: Why should you clean up event listeners and timers in
onUnmounted?
-
Q: What is the difference between
onMountedandonBeforeMount?
9. Summary
Vue 3's lifecycle hooks provide precise control over each phase of a component's life.onMounted is the most important — it's where data fetching and DOM initialization belong. onUnmounted is equally important — every resource acquired in onMounted should be released in onUnmounted to prevent memory leaks.