Chapter 49: Vue KeepAlive Component

The <KeepAlive> component

This is one of the built-in special components that Vue gives you for free — no installation, no import — and once you understand it, you’ll wonder how you ever built tabbed interfaces, multi-step wizards, or dashboards without it.

What does <KeepAlive> actually do? (Very simple human explanation)

Normally when a component is hidden (via v-if, router navigation, dynamic :is, etc.), Vue destroys it completely:

  • All internal state is lost
  • All DOM is removed
  • All event listeners are cleaned up
  • Next time it appears → it mounts from scratch (re-runs setup, fetches data again, resets forms, scrolls to top, etc.)

<KeepAlive> changes this behavior:

It keeps the component alive in memory even when it’s not visible on the screen.

So when the component is shown again:

  • It does NOT re-mount / re-run setup
  • It keeps its internal state (form values, checked checkboxes, scroll position, fetched data…)
  • It keeps its DOM tree (just hidden)
  • Vue only activates/deactivates it (runs special activated / deactivated hooks)

This creates the feeling of a native multi-page app even though it’s still a single-page application (SPA).

Most Common & Most Valuable Use-Cases (2026)

Use-case Without <KeepAlive> With <KeepAlive> Why users love it
Tabs (dashboard, settings, profile) Switch tab → form resets, data refetched Switch tab → form stays filled, scroll position saved Huge UX win
Router-view with many routes Navigate back → list resets to top, filters lost Navigate back → exact same scroll position & filters Feels native
Multi-step wizard / checkout flow Go back → previous step resets Go back → all entered data preserved Prevents rage-quit
Chat / messaging app Switch conversation → message list resets Switch back → exact scroll position & unread state saved Feels like real chat
Data-heavy tabs (charts, tables) Switch tab → chart re-renders, API called again Switch back → chart stays exactly as left (zoomed, etc.) Saves time & API calls

Real, Practical Example – Tabbed Dashboard with <KeepAlive>

vue

Now let’s create one of the tab components that has state (a form) — so you can see the difference.

Analytics.vue (has form + scrollable content)

vue

What happens without <KeepAlive>

  • Switch to Overview → Analytics unmounts → form resets to defaults, scroll goes to top
  • Switch back → Analytics re-mounts → API would be called again, form empty, scroll at top

With <KeepAlive>

  • Switch away → Analytics cached in memory
  • Switch back → exact same state: form values preserved, scroll position preserved, no re-fetch

Extra Features & Control (2026 Details)

Feature Syntax / Prop What it does / When to use
Cache only specific components <KeepAlive include=”Overview,Analytics”> Only cache named components
Exclude some <KeepAlive exclude=”HeavyChart”> Prevent caching heavy ones
Max cached instances <KeepAlive max=”10″> Limit memory usage
Check if cached onActivated(() => console.log(‘Tab activated’)) Run code when tab shown again
Check when deactivated onDeactivated(() => console.log(‘Tab hidden’)) Pause timers, save draft, etc.

Summary Table – When to Use <KeepAlive>

Situation Use <KeepAlive>? Why / Alternative
Tabs with forms, filters, scroll Yes Preserves UX & state
Wizard / checkout steps Yes Don’t lose entered data
Router-view with many pages Yes (around <router-view>) Back/forward feels native
Very heavy component (charts, maps) Usually no Better to re-mount and re-fetch
Modal / dialog No Should always start fresh
List with pagination / infinite scroll Usually no State belongs to parent

Pro Tips from Real Projects (Hyderabad 2026)

  • Always test mobile — scroll position preservation feels amazing on touch devices
  • Combine with <Transition> for smooth tab switch animations
  • Use onActivated / onDeactivated instead of onMounted/onUnmounted when inside <KeepAlive>
  • For very large apps → limit max prop to avoid memory issues
  • Don’t wrap everything in <KeepAlive> — it keeps components in memory forever

Your mini practice task:

  1. Build a simple 3-tab dashboard (Overview, Settings, Profile)
  2. Put <KeepAlive> around the dynamic <component :is>
  3. Add a form + long scrollable content in Settings tab
  4. Switch tabs → go back → see that form values & scroll position are preserved

Any part confusing? Want full examples for:

  • <KeepAlive> + <router-view> + transition?
  • onActivated / onDeactivated with timers & API polling?
  • <KeepAlive> + <Transition> for animated tab switch?
  • Real dashboard with lazy-loaded tabs?

Just tell me — we’ll build the next smooth, cached tab system together 🚀

You may also like...

Leave a Reply

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