0
0
Vueframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Keep-alive for expensive components
Component mounts first time
Component renders and runs setup
Component cached by <keep-alive>
User navigates away
Component is hidden but state kept
User navigates back
Component restored from cache, no re-setup
Component continues with preserved state
This flow shows how Vue's <keep-alive> caches a component after first render, preserving its state when hidden and restoring it without re-running setup.
Execution Sample
Vue
<template>
  <keep-alive>
    <ExpensiveComponent v-if="show" />
  </keep-alive>
  <button @click="show = !show">Toggle</button>
</template>

<script setup>
import { ref } from 'vue'
import ExpensiveComponent from './ExpensiveComponent.vue'
const show = ref(true)
</script>
This Vue code toggles an expensive component inside <keep-alive>, caching it to avoid re-setup on toggle.
Execution Table
StepActionComponent StateCache StatusRendered Output
1Initial render, show=trueExpensiveComponent setup runsCache emptyExpensiveComponent visible
2User clicks toggle, show=falseExpensiveComponent hidden but aliveComponent cachedNo component visible
3User clicks toggle, show=trueExpensiveComponent restored from cacheComponent cachedExpensiveComponent visible with preserved state
4User clicks toggle, show=false againComponent hidden, state preservedComponent cachedNo component visible
5User clicks toggle, show=true againComponent restored, no setup rerunComponent cachedExpensiveComponent visible with same state
6User navigates away, component unmountedComponent destroyedCache cleared if <keep-alive> removedNo component visible
💡 Execution stops when component is unmounted or <keep-alive> removed, clearing cache.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
showtruetruefalsetruefalsetruefalse
ExpensiveComponent statenoneinitializedpreserved (hidden)restoredpreserved (hidden)restoreddestroyed
Cacheemptystores componentstores componentstores componentstores componentstores componentcleared
Key Moments - 3 Insights
Why doesn't ExpensiveComponent run setup again when toggled back on?
Because <keep-alive> caches the component after first mount (see Step 3 in execution_table), so it restores the component from cache without rerunning setup.
What happens to the component state when it is hidden by v-if inside <keep-alive>?
The component is hidden but kept alive and its state is preserved in cache (see Step 2 and Step 4), so when shown again, it resumes with the same state.
When is the cached component destroyed and removed from memory?
When the component is unmounted or <keep-alive> is removed from the template (see Step 6), the cache clears and the component is destroyed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cache status after the user first hides the component (Step 2)?
ACache cleared
BCache empty
CComponent cached
DComponent destroyed
💡 Hint
Check the 'Cache Status' column at Step 2 in the execution_table.
At which step does the ExpensiveComponent restore from cache without running setup again?
AStep 1
BStep 3
CStep 6
DStep 2
💡 Hint
Look at the 'Component State' column describing setup or restore in the execution_table.
If the <keep-alive> wrapper is removed, what happens to the component when toggled off?
AComponent is destroyed and state lost
BComponent is hidden but state preserved
CComponent is cached and preserved
DComponent setup runs again immediately
💡 Hint
Refer to Step 6 and the explanation about cache clearing when is removed.
Concept Snapshot
Vue <keep-alive> caches components to preserve state.
Wrap expensive components with <keep-alive>.
When toggled off, component is hidden but kept alive.
When toggled on again, component restores without re-setup.
Cache clears only when component unmounts or <keep-alive> removed.
Full Transcript
This visual execution shows how Vue's <keep-alive> works to cache expensive components. Initially, the component mounts and runs its setup. When the user hides it using v-if, the component is not destroyed but hidden and cached. Later, when shown again, the component restores from cache without running setup again, preserving its state. The cache remains until the component is unmounted or <keep-alive> is removed, at which point the component is destroyed and cache cleared. This helps improve performance by avoiding repeated expensive setup work.