0
0
Vueframework~10 mins

Keep-alive for caching components in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Keep-alive for caching components
Component A Rendered
Switch to Component B
Component A Cached
Component B Rendered
Switch back to Component A
Component A Restored from Cache
Component B Cached
When switching components inside <keep-alive>, Vue caches inactive components instead of destroying them, so switching back restores their state instantly.
Execution Sample
Vue
<template>
  <keep-alive>
    <component :is="currentComponent" />
  </keep-alive>
</template>

<script setup>
import { ref } from 'vue'
const currentComponent = ref('CompA')
</script>
This code switches between components inside <keep-alive>, caching inactive ones to preserve their state.
Execution Table
StepActionCurrent ComponentCache StateRendered Output
1Initial renderCompACache emptyCompA content shown
2Switch to CompBCompBCompA cachedCompB content shown
3Switch back to CompACompACompB cachedCompA restored from cache
4Switch to CompB againCompBCompA cachedCompB restored from cache
5End of demoCompBCompA cachedCompB content shown
💡 Demo ends after switching components and caching states.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
currentComponentCompACompACompBCompACompBCompB
cacheemptyemptyCompA cachedCompB cachedCompA cachedCompA cached
Key Moments - 2 Insights
Why does switching back to CompA show its previous state instead of resetting?
Because <keep-alive> caches CompA when switching away (see Step 2), so when switching back (Step 3), Vue restores it from cache instead of recreating.
What happens to the component not currently shown?
It is kept in cache by <keep-alive> (see Cache State column), so it is not destroyed but stored for quick restoration.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 2. What is the cache state after switching to CompB?
ACache empty
BCompB cached
CCompA cached
DBoth CompA and CompB cached
💡 Hint
Check the 'Cache State' column at Step 2 in the execution table.
At which step does CompA get restored from cache?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Rendered Output' column for when CompA is restored.
If <keep-alive> was removed, what would happen when switching components?
AComponents would still cache their state
BComponents would be destroyed and recreated each switch
COnly the first component would render
DVue would throw an error
💡 Hint
Think about what does as shown in the concept flow and execution table.
Concept Snapshot
<keep-alive> wraps dynamic components to cache inactive ones.
Switching components inside it preserves their state.
Cached components are not destroyed but hidden.
Restoring a cached component is instant.
Useful for tabs, wizards, or expensive components.
Full Transcript
This visual trace shows how Vue's <keep-alive> caches components. Initially, CompA renders and cache is empty. When switching to CompB, CompA is cached and CompB renders. Switching back restores CompA from cache instantly. The cache always holds the inactive component. This avoids destroying and recreating components, preserving their state and improving performance. Removing <keep-alive> would cause components to be destroyed on switch. This is useful for UI patterns like tabs where you want to keep user input or scroll position.