Chapter 107: Vue ‘deactivated’ Lifecycle Hook

Deactivated

(and its modern Composition API name onDeactivated)

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

It exists exclusively when a component is wrapped inside <KeepAlive>.

Together with its sibling activated / onActivated, these two hooks are the “alive” part of KeepAlive.

1. Mental model – When & why deactivated exists

Normal behavior (without <KeepAlive>):

text

→ Every time the component disappears and reappears → new instance, state lost, timers restarted, expensive re-init, scroll position reset, form values cleared, etc.

With <KeepAlive>:

text

→ The component never really dies — it keeps its:

  • internal state (ref values, reactive objects)
  • watchers
  • timers / intervals
  • event listeners
  • third-party instances (charts, maps, video players…)
  • scroll positions
  • form inputs values
  • etc.

deactivated = “I’m being hidden / cached → time to pause / save / stop temporary things”

activated = “I’m being shown again → time to resume / restore / restart”

2. Real situations where you need deactivated / onDeactivated

Situation What happens without deactivated? What you usually do in deactivated / activated
Tab content / router-view inside <KeepAlive> Every tab switch → full re-mount → state lost, API called again, scroll reset deactivated → pause polling/WebSocket/video, save form draft, save scroll position activated → resume polling, restore scroll, refocus input
Drawer / sidebar / off-canvas menu Close drawer → re-opening creates new instance deactivated → pause animations, stop audio previews activated → restart animation, refocus first input
Modal inside <KeepAlive> Re-opening modal → new instance → focus lost, form cleared deactivated → save draft, pause video activated → restore form, refocus first field
Chat window / infinite scroll feed Switch route/tab → scroll position lost deactivated → save scrollTop activated → restore scrollTop
Video / audio player inside cached tab Switch tab → video stops / restarts from 0 deactivated → pause playback, save currentTime activated → resume playback from saved time
Real-time data feed / WebSocket Switch away → connection closed unnecessarily deactivated → pause / unsubscribe temporarily activated → reconnect / resume subscription

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

App.vue (wraps tabs in <KeepAlive>)

vue

TabChat.vue – shows onActivated / onDeactivated in action

vue

4. 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 / cached
Does it run on first mount? Yes (right after mounted) No (only when hidden first time)
Does component re-mount? No – cached 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/video/animation Save scroll, pause polling/video, blur input, save draft
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 / chat feeds inside <KeepAlive>
  • In Nuxt / Vue Router → onActivated / onDeactivated are essential for keeping page state when reusing components
  • Combine with nextTick() in onActivated when restoring scroll / focus (DOM re-insertion timing can vary)
  • Never do one-time initialization in onActivated — use onMounted for that

Your mini homework:

  1. Create the tabbed interface above with <KeepAlive>
  2. Add long scrollable content + textarea in one tab
  3. Switch tabs multiple times → verify textarea content & scroll position are preserved
  4. Add console.log in onActivated / onDeactivated → observe timing

Any part confusing? Want full examples for:

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

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

You may also like...

Leave a Reply

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