Explain it simply
Lifecycle Hooks, in plain language
Every component passes through create, mount, update, and unmount stages. Run any code that needs the DOM inside onMounted. Clean up timers, event listeners, and subscriptions in onUnmounted.
Problem → Why → Solution
The problem — why this concept matters
Not cleaning up event listeners
A common mistake
Accessing a DOM element inside setup before it's actually mounted
Core concept — understand how it works
Run code at the right point in a component's lifecycle using onMounted, onUpdated, and onUnmounted hooks.
- props or event
- component and template
- rendered Vue UI
Understand when mounting and unmounting happen
Core syntax or pattern
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
const width = ref(0)
const measure = () => { width.value = window.innerWidth }
onMounted(() => {
measure()
window.addEventListener('resize', measure)
})
onUnmounted(() => window.removeEventListener('resize', measure))
</script>
<template><p>Viewport: {{ width }}px</p></template>Run code at the right point in a component's lifecycle using onMounted, onUpdated, and onUnmounted hooks.
Read the code step by step
- props or event
<script setup>Every component passes through create, mount, update, and unmount stages. Run any code that needs the DOM inside onMounted. Clean up timers, event listeners, and subscriptions in onUnmounted.
- component and template
measure()Run code at the right point in a component's lifecycle using onMounted, onUpdated, and onUnmounted hooks.
- rendered Vue UI
<template><p>Viewport: {{ width }}px</p></template>Apply Lifecycle Hooks in a small example and test normal, edge, and failure cases.
- props or event
- component and template
- rendered Vue UI
Success
Understand when mounting and unmounting happen
Failure
Not cleaning up event listeners
Let's break it down simply
Every component passes through create, mount, update, and unmount stages. Run any code that needs the DOM inside onMounted. Clean up timers, event listeners, and subscriptions in onUnmounted.
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
const width = ref(0)
const measure = () => { width.value = window.innerWidth }
onMounted(() => {
measure()
window.addEventListener('resize', measure)
})
onUnmounted(() => window.removeEventListener('resize', measure))
</script>
<template><p>Viewport: {{ width }}px</p></template>Viewport: 1280pxTry it yourself
Write a timer component that increments every second, and clear the interval when it unmounts.
Lifecycle Hooks — Vue.js
Real-world usage
How to apply it in a real project
Apply Lifecycle Hooks in a small example and test normal, edge, and failure cases.
Mini challenge
Build it yourself
Write a timer component that increments every second, and clear the interval when it unmounts.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
const width = ref(0)
const measure = () => { width.value = window.innerWidth }
onMounted(() => {
measure()
window.addEventListener('resize', measure)
})
onUnmounted(() => window.removeEventListener('resize', measure))
</script>
<template><p>Viewport: {{ width }}px</p></template>Expected: Understand when mounting and unmounting happen
Show hint
Apply Lifecycle Hooks in a small example and test normal, edge, and failure cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- props or event → component and template → rendered Vue UI
- Run code at the right point in a component's lifecycle using onMounted, onUpdated, and onUnmounted hooks.
- Not cleaning up event listeners
- Apply Lifecycle Hooks in a small example and test normal, edge, and failure cases.
Lifecycle Hooks — Vue.js