100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

The Vue Instance Lifecycle

An overview of the stages a Vue component instance goes through from creation to unmounting, and the lifecycle hooks available at each stage.

Vue FoundationsBeginner9 min readJul 9, 2026
Analogies

The Vue Instance Lifecycle

Every Vue component instance goes through a predictable sequence of stages: creation, template compilation, DOM mounting, reactive updates, and eventual unmounting. Vue exposes lifecycle hooks — functions you can register to run at specific points in this sequence — so you can perform setup work like fetching data, attaching event listeners, or initializing third-party libraries, and cleanup work like clearing timers or removing listeners before the component is destroyed.

🏏

Cricket analogy: Like a cricketer's career following a predictable arc — selection trials, training camp, debut match, ongoing seasons, and eventual retirement — with specific checkpoints (lifecycle hooks) where the board schedules a fitness test before debut or arranges a farewell tour before retirement.

The Mounting Sequence

When a component is created, Vue first sets up reactive state (props, data, computed properties) — in the Composition API this happens implicitly as the setup() function or <script setup> block runs. Next comes the mounting phase: Vue compiles the template into a render function (if not precompiled), creates the initial DOM elements, and inserts them into the page. The onBeforeMount hook fires right before this DOM insertion, and onMounted fires immediately after, once the component's DOM is fully in place — this is the correct place to access the DOM via template refs or to initiate data fetching that needs the rendered layout.

🏏

Cricket analogy: Like a stadium's setup crew first preparing the pitch and equipment (reactive state), then compiling the day's fielding plan into an actual positioning script, placing fielders on the ground right before play — the umpire signals 'ready' just before players take the field (onBeforeMount) and 'play' is called right after everyone is in position (onMounted), which is exactly when the coach can start reading live field spacing.

Update and Unmount Phases

After mounting, whenever reactive state used in the template changes, Vue schedules a re-render. onBeforeUpdate fires before the DOM is patched with new values, and onUpdated fires after the patch completes — these are used sparingly, typically for measuring or reacting to DOM changes after a re-render. Finally, when a component is removed from the tree (for example, navigating away in Vue Router, or a v-if becoming false), Vue runs the unmounting phase: onBeforeUnmount fires while the component is still fully functional, and onUnmounted fires after it has been torn down. Cleanup — removing event listeners, clearing intervals, cancelling network requests — belongs in onBeforeUnmount or onUnmounted.

🏏

Cricket analogy: Like a scoreboard operator who gets a heads-up just before updating the display after a boundary (onBeforeUpdate) and confirms the new total is live right after (onUpdated), and later, when the match ends and the ground is being vacated (unmounting), the PA system is switched off and the floodlight timers are cancelled during teardown (onBeforeUnmount/onUnmounted) rather than left running.

vue
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'

const windowWidth = ref(window.innerWidth)

function updateWidth() {
  windowWidth.value = window.innerWidth
}

onMounted(() => {
  window.addEventListener('resize', updateWidth)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', updateWidth)
})
</script>

<template>
  <p>Window width: {{ windowWidth }}px</p>
</template>

In the Composition API, lifecycle hooks are imported functions (onMounted, onUpdated, onUnmounted, etc.) called directly inside setup() or <script setup>, rather than object properties as in the Options API (mounted(), updated(), unmounted()). Both styles map to the exact same underlying lifecycle — only the syntax for registering the callback differs.

There is no onCreated hook in the Composition API — code that would run in the Options API's created() hook simply runs as top-level code in setup() or <script setup>, since that code executes synchronously during instance creation, before mounting.

  • The Vue lifecycle moves through creation, mounting, updating, and unmounting phases.
  • onMounted runs after the component's DOM has been inserted — ideal for DOM access and data fetching.
  • onBeforeUpdate/onUpdated bracket reactive re-renders triggered by state changes.
  • onBeforeUnmount/onUnmounted bracket component teardown — the right place for cleanup.
  • Composition API hooks are imported functions called inside setup()/<script setup>.
  • There is no direct onCreated equivalent — that logic is just top-level setup() code.

Practice what you learned

Was this page helpful?

Topics covered

#JavaScript#VueJsStudyNotes#WebDevelopment#TheVueInstanceLifecycle#Vue#Instance#Lifecycle#Mounting#StudyNotes#SkillVeris