Chapter 35: Vue Lifecycle Hooks

Vue Lifecycle Hooks

These are special functions (or methods) that Vue automatically calls at specific points during a component’s life — from creation → mounting → updating → destruction.

Understanding lifecycle hooks is like knowing when you can safely do certain things:

  • Fetch data when component appears on screen
  • Set up timers / event listeners when component is ready
  • Clean up timers / listeners before component disappears
  • React to prop or data changes
  • Run code only once when component is first created

In Vue 3 (2026 standard) there are two completely different ways to use lifecycle hooks, depending on whether you use Options API or Composition API (<script setup>).

Both are still very common in 2026 — so you need to know both.

1. Options API – Classic / Legacy Style (still everywhere in old projects)

vue

2. Composition API – Modern & Recommended (2026 standard)

In <script setup> we use imported functions from ‘vue’.

These functions are only called once (except onUpdated & onBeforeUpdate).

vue

Real, Practical Example – Timer & Fetch with Proper Cleanup

vue

Full Lifecycle Order (Vue 3 – 2026)

Parent → Child order matters

  1. setup() (Composition API) or beforeCreate + created (Options)
  2. Parent onBeforeMount
  3. Child onBeforeMount
  4. Child onMounted
  5. Parent onMounted
  6. (Later) Data change → onBeforeUpdate (parent → child) → onUpdated (child → parent)
  7. When removed → onBeforeUnmount (parent → child) → onUnmounted (child → parent)

Quick Summary Table – When to Use Each Hook (2026 Guide)

Hook (Composition) Options API equivalent Typical Use Cases (2026) Run Once or Multiple?
onBeforeMount beforeMount Rare – prepare before first DOM render Once
onMounted mounted Fetch data, set up listeners, focus inputs, init 3rd-party libs Once
onBeforeUpdate beforeUpdate Rare – read old DOM before re-render Multiple
onUpdated updated DOM-dependent operations after update (rare) Multiple
onBeforeUnmount beforeUnmount Clean up: clearInterval, removeEventListener, cancel fetch, dispose libs Once
onUnmounted unmounted Final cleanup / analytics (component gone) Once
onErrorCaptured errorCaptured Global error boundary for child tree Multiple
onServerPrefetch Nuxt / SSR data fetching before render Once (SSR only)

Pro Tips from Real Projects (Hyderabad 2026)

  • Most important hooks → onMounted & onBeforeUnmount
  • Always clean up in onBeforeUnmount — memory leaks are very common otherwise
  • Use nextTick() inside onMounted if you need to wait for child components to mount
  • For data fetching → prefer TanStack Query or onServerPrefetch (Nuxt) over raw onMounted
  • Don’t fetch in setup() — it’s not reactive-aware & runs on server too

Your mini practice:

  1. Create component with timer that starts in onMounted
  2. Clear timer in onBeforeUnmount
  3. Add fetch call in onMounted with loading/error states
  4. Add console logs in all hooks to see order

Any part confusing? Want to see:

  • Lifecycle with <KeepAlive> (cached components)?
  • Lifecycle + async setup / suspense?
  • Full auth flow with token refresh in lifecycle?
  • Common memory leak examples & how to fix?

Just tell me — we’ll debug and build together step by step 🚀

Happy lifecycle mastering from Hyderabad! 💙

You may also like...

Leave a Reply

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