Chapter 106: Vue ‘activated’ Lifecycle Hook

Activated

(and its sibling deactivated)

These two hooks exist only when a component is wrapped in <KeepAlive>.

They have nothing to do with the normal lifecycle of a component that is simply shown/hidden with v-if / v-show.

They are the “alive” part of KeepAlive.

1. Mental model – When & why activated / deactivated exist

Normal lifecycle (without <KeepAlive>):

text

→ Every time the component disappears and reappears → new instance, new mounted, state lost, timers/listeners recreated, expensive re-init

With <KeepAlive>:

text

→ Component never really dies → keeps its internal state, watchers, timers, DOM nodes, third-party instances… → Only gets temporarily hidden and later re-shown

activated = “I was hidden → now I’m visible again → time to wake up / resume / re-attach listeners / restart animations / refocus / etc.”

deactivated = “I’m about to be hidden → time to pause / cleanup temporary things / save scroll position / stop intervals / etc.”

2. Real-world situations where you need activated / deactivated

Situation What happens without activated/deactivated? What you usually do in activated / deactivated
Tab / router-view content Every tab switch → full re-mount → state lost, API called again, scroll reset activated → restore scroll position, restart polling deactivated → pause polling, save form draft
Modal / dialog / drawer inside <KeepAlive> Re-opening modal → new instance → focus lost, form cleared activated → focus first input, restart animation deactivated → save draft, stop video
Infinite scroll / chat window Switch tab → scroll position lost activated → scroll back to saved position deactivated → save current scrollTop
Video player / audio player Switch tab → video stops / restarts activated → resume playback deactivated → pause playback
Real-time feed / WebSocket connection Switch away → connection closed activated → reconnect / resume subscription deactivated → pause / unsubscribe
Expensive third-party widget (map, chart) Switch away → widget reinitialized activated → refresh / resize widget deactivated → pause updates

3. Real, Practical Example – Tabbed Interface with Preserved State

vue

TabProfile.vue – shows activated / deactivated in action

vue

What happens when you switch tabs:

  • First time Profile tab shown → mounted + activated
  • Switch to another tab → deactivated → saves scroll position & bio
  • Switch back → activated → restores scroll position & bio is still there
  • No new mounted call → component instance stayed alive

Quick Summary Table – activated / deactivated in 2026

Question activated / onActivated deactivated / onDeactivated
When does it run? When cached component becomes visible again When cached component is hidden
Does it run on first mount? Yes (after mounted) No (only when hidden first time)
Does component re-mount? No – instance reused No – instance kept alive
State / watchers / timers? All preserved All preserved (until you pause them)
DOM / refs available? Yes – DOM re-inserted Yes – DOM still there (but hidden)
Most common use Restore scroll, refocus input, resume polling, restart animation Save scroll, pause polling/video, blur input
Do modern developers use it? Very often – in tabs, drawers, modals with <KeepAlive> Very often – same scenarios

Pro Tips from Real Projects (Hyderabad 2026)

  • Most common pattern: <KeepAlive> + tabs/router-view + onActivated / onDeactivated
  • Use onActivated to resume things: refocus input, restart polling/WebSocket, resume video, restore scroll
  • Use onDeactivated to pause things: pause video, stop polling, save draft, blur input
  • Always save/restore scroll position in tabs / infinite lists inside <KeepAlive>
  • In Nuxt / Vue Router → onActivated / onDeactivated are essential for keeping state when reusing page components
  • Combine with nextTick() in onActivated when restoring scroll / focus (DOM re-insertion timing)
  • Never do one-time init in onActivated — use onMounted for that

Your mini homework:

  1. Create the tabbed interface above with <KeepAlive>
  2. Add input + long scrollable content in one tab
  3. Switch tabs → see state (input value, scroll position) preserved
  4. Add console.log in onActivated / onDeactivated → watch timing

Any part confusing? Want full examples for:

  • onActivated + scroll restoration in infinite chat / feed?
  • onDeactivated + pause/resume video player?
  • <KeepAlive> + router-view + activated pattern in Vue Router?
  • activated / deactivated vs mounted / unmounted comparison?

Just tell me — we’ll build the next preserved-state tab / drawer system together step by step 🚀

You may also like...

Leave a Reply

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