Thuta Learning
Vue.js
IntermediateWeb DevelopmentBeginner

Lifecycle Hooks

Vue.jsLesson 1420 min

What you'll walk away with

  • Understand when mounting and unmounting happen
  • Write cleanup logic

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.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Understand when mounting and unmounting happen

Core syntax or pattern

vue
<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

  1. 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.

  2. component and templatemeasure()

    Run code at the right point in a component's lifecycle using onMounted, onUpdated, and onUnmounted hooks.

  3. rendered Vue UI<template><p>Viewport: {{ width }}px</p></template>

    Apply Lifecycle Hooks in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. 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.

vue
<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>
You should see
Viewport: 1280px

Try 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
vue
<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

What best describes the core idea of Lifecycle Hooks?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Not cleaning up event listeners
  • Accessing a DOM element inside setup before it's actually mounted

Exercise

Write a timer component that increments every second, and clear the interval when it unmounts.

You'll know it worked when: Viewport: 1280px

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Lifecycle Hooks | Thuta Learning