Chapter 94: Vue Lifecycle Hooks

Vue Lifecycle Hooks

These are special named functions (or callbacks) that Vue automatically calls at very precise moments during the life of a component instance — from creation → mounting → updating → destruction.

They are your chance to run code exactly when something important happens to the component.

Understanding lifecycle hooks is what separates people who write working Vue code from people who write robust, performant, bug-free Vue code.

1. The Full Lifecycle – Visual Timeline (Memorize This Sequence)

Here’s the complete order (Vue 3 – 2026 standard):

text

Modern Composition API names (what you use in <script setup>):

TypeScript

2. Real, Practical Example – Full Component Showing Every Hook

vue

ChildComponent.vue (to see mount/unmount)

vue

What you see in console when you interact:

  1. Page loads → onBeforeMount → onMounted
  2. Click “+1” multiple times → onBeforeUpdate → onUpdated each time
  3. Click “Hide Child” → onBeforeUnmount (child) → onUnmounted (child)
  4. Click “Show Child” again → child onMounted again
  5. Click “Hide” again → child onBeforeUnmount & onUnmounted

3. The Most Important Hooks – When to Use Each (2026 Rules)

Hook When it runs Best for (real-world) Never do this here
onBeforeMount After setup, before first DOM insert Very rarely – almost never needed Access refs / DOM
onMounted After first render, DOM is ready Fetch initial data, add event listeners, focus inputs, init 3rd-party libs (Chart.js, Mapbox, Swiper…)
onBeforeUpdate Data changed, before re-render/patch Debugging, measuring before DOM update (rare) Mutate data
onUpdated After re-render/patch Measure DOM after update (e.g. height change) Mutate data (loop risk)
onBeforeUnmount Before removal from DOM Cleanup: removeEventListener, clearInterval, cancel fetch/abort, remove resize observer
onUnmounted After removal, instance is dead Final cleanup (rare – most done in beforeUnmount) Access refs / DOM

4. Quick Summary Table – Lifecycle Hooks in 2026

Hook Options API name Composition API name Runs multiple times? DOM ready? Typical real use-case
beforeCreate beforeCreate (almost never used) No No Very rare
created created (almost never used) No No Very rare
beforeMount beforeMount onBeforeMount No No Rare
mounted mounted onMounted No Yes Fetch data, init libs
beforeUpdate beforeUpdate onBeforeUpdate Yes Yes Debugging
updated updated onUpdated Yes Yes DOM measurement after update
beforeUnmount beforeUnmount onBeforeUnmount No Yes Cleanup (timers, listeners)
unmounted unmounted onUnmounted No No Final cleanup (rare)

Pro Tips from Real Projects (Hyderabad 2026)

  • Most used hooks: onMounted (init), onBeforeUnmount (cleanup), onUpdated (rare DOM measurement)
  • Never mutate state in onUpdated — causes infinite loop
  • Always cleanup in onBeforeUnmount — timers, event listeners, AbortController, ResizeObserver, etc.
  • Use onMounted for DOM access / refs / 3rd-party libs — not in setup()
  • In server-side rendering (SSR / Nuxt) → onMounted runs only client-side
  • Combine onMounted + nextTick() when you need DOM right after first render

Your mini homework:

  1. Create the component above
  2. Toggle the child → watch console logs for mount/unmount
  3. Add onBeforeUnmount to remove a fake timer
  4. Add onUpdated to log DOM height after count changes

Any part confusing? Want full examples for:

  • Full lifecycle log + cleanup timer example?
  • onMounted + third-party lib (Chart.js / Mapbox)?
  • onBeforeUnmount + event listener / ResizeObserver cleanup?
  • Lifecycle in <Transition> / <KeepAlive>?

Just tell me — we’ll trace the lifecycle of the next component together 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *