Bird
Raised Fist0
Vueframework~20 mins

Keep-alive for caching components in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Keep-alive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a component is wrapped with in Vue?

Consider a Vue app where a component is wrapped inside a tag. What is the main effect of this wrapping on the component's lifecycle?

Vue
<template>
  <keep-alive>
    <MyComponent v-if="show" />
  </keep-alive>
</template>

<script setup>
import { ref } from 'vue'
import MyComponent from './MyComponent.vue'
const show = ref(true)
</script>
AThe component's template is cached but its data is reset every time it is shown.
BThe component is destroyed immediately when toggled off and recreated fresh when toggled on.
CThe component is cached and its state is preserved when toggled off and on.
DThe component is rendered twice to improve performance.
Attempts:
2 left
💡 Hint

Think about what caching means for a component's state and lifecycle.

❓ state_output
intermediate
2:00remaining
What is the value of count after toggling a cached component off and on?

Given the component below, what will be the value of count after toggling show off and then on again?

Vue
<template>
  <keep-alive>
    <Counter v-if="show" />
  </keep-alive>
  <button @click="show = !show">Toggle</button>
</template>

<script setup>
import { ref } from 'vue'
import Counter from './Counter.vue'
const show = ref(true)
</script>

<!-- Counter.vue -->
<template>
  <button @click="count++">Count: {{ count }}</button>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
AThe count value remains the same as before toggling off.
BThe count resets to zero after toggling off and on.
CThe count increments automatically when toggled on again.
DThe count value becomes undefined after toggling.
Attempts:
2 left
💡 Hint

Remember what does to component state.

❓ lifecycle
advanced
2:00remaining
Which lifecycle hooks are called when a component is toggled off and then on again?

Consider a component wrapped in . When the component is toggled off (hidden) and then toggled on (shown) again, which lifecycle hooks run?

Vue
<template>
  <keep-alive>
    <MyComponent v-if="show" />
  </keep-alive>
  <button @click="show = !show">Toggle</button>
</template>

<script setup>
import { ref } from 'vue'
import MyComponent from './MyComponent.vue'
const show = ref(true)
</script>
Amounted when first shown, then activated and deactivated on toggle, but not unmounted or created again.
Bcreated and mounted every time it is toggled on, destroyed when toggled off.
Cactivated and deactivated only once when the app loads.
Donly mounted once and never activated or deactivated.
Attempts:
2 left
💡 Hint

Think about the special lifecycle hooks activated and deactivated in Vue.

📝 Syntax
advanced
2:00remaining
Which option correctly uses with include prop to cache only specific components?

You want to cache only components named 'Home' and 'Profile' using . Which syntax is correct?

A
&lt;keep-alive include="['Home', 'Profile']"&gt;
  &lt;component :is="currentView" /&gt;
&lt;/keep-alive&gt;
B
&lt;keep-alive include="Home,Profile"&gt;
  &lt;component :is="currentView" /&gt;
&lt;/keep-alive&gt;
C
&lt;keep-alive include={[Home, Profile]}&gt;
  &lt;component :is="currentView" /&gt;
&lt;/keep-alive&gt;
D
&lt;keep-alive :include="['Home', 'Profile']"&gt;
  &lt;component :is="currentView" /&gt;
&lt;/keep-alive&gt;
Attempts:
2 left
💡 Hint

Check how to bind props dynamically in Vue templates.

🔧 Debug
expert
3:00remaining
Why does a cached component not update when its props change?

You wrapped a component with to cache it. However, when the parent changes the props passed to this component, the component does not update its display. What is the most likely cause?

Vue
<template>
  <keep-alive>
    <MyComponent :value="parentValue" />
  </keep-alive>
  <button @click="parentValue++">Change Value</button>
</template>

<script setup>
import { ref } from 'vue'
import MyComponent from './MyComponent.vue'
const parentValue = ref(0)
</script>
AProps cannot be passed to components inside <keep-alive>.
BThe component caches its state and does not react to prop changes unless a key is used to force re-render.
CThe <keep-alive> disables all reactive updates inside the component.
DThe parentValue ref is not reactive, so changes are not detected.
Attempts:
2 left
💡 Hint

Consider how Vue tracks component identity and updates with .

Practice

(1/5)
1. What is the main purpose of the <keep-alive> component in Vue?
easy
A. To cache components and preserve their state when switching views
B. To permanently remove components from the DOM
C. To reload components every time they are displayed
D. To style components with CSS automatically

Solution

  1. Step 1: Understand what <keep-alive> does

    The <keep-alive> component caches inactive components instead of destroying them.
  2. Step 2: Identify the effect on component state

    Because components are cached, their state (like user input or scroll position) is preserved when switching back.
  3. Final Answer:

    To cache components and preserve their state when switching views -> Option A
  4. Quick Check:

    Keep-alive caches components [OK]
Hint: Remember: keep-alive means keep the component's state alive [OK]
Common Mistakes:
  • Thinking keep-alive reloads components every time
  • Confusing keep-alive with styling or layout features
  • Assuming keep-alive removes components permanently
2. Which of the following is the correct syntax to use keep-alive with a dynamic component named MyComponent?
easy
A. <keep-alive><component :is="MyComponent" /></keep-alive>
B. <keep-alive :is="MyComponent" />
C. <component :is="MyComponent" keep-alive />
D. <keep-alive><MyComponent /></keep-alive>

Solution

  1. Step 1: Recall how to wrap dynamic components with keep-alive

    The <keep-alive> component wraps around a dynamic component using <component :is="..." />.
  2. Step 2: Check each option's syntax

    <keep-alive><component :is="MyComponent" /></keep-alive> correctly wraps <component :is="MyComponent" /> inside <keep-alive>. Options A and C misuse attributes or tags. <keep-alive><MyComponent /></keep-alive> tries to wrap a component tag directly, which is invalid for dynamic components.
  3. Final Answer:

    <keep-alive><component :is="MyComponent" /></keep-alive> -> Option A
  4. Quick Check:

    Wrap dynamic component inside keep-alive [OK]
Hint: Wrap inside tags [OK]
Common Mistakes:
  • Trying to add keep-alive as a prop to component
  • Using component tags directly without :is for dynamic components
  • Placing :is on keep-alive instead of component
3. Given this Vue template:
<keep-alive>
  <component :is="currentView" />
</keep-alive>

and currentView switches between 'ViewA' and 'ViewB'. What happens to the state of 'ViewA' when switching to 'ViewB' and back?
medium
A. The state of 'ViewA' is reset to initial values
B. 'ViewA' state is preserved because it is cached
C. An error occurs because keep-alive cannot cache dynamic components
D. 'ViewA' is destroyed and recreated fresh

Solution

  1. Step 1: Understand keep-alive behavior with dynamic components

    When wrapped in <keep-alive>, inactive components are cached, not destroyed.
  2. Step 2: Apply this to switching views

    Switching from 'ViewA' to 'ViewB' caches 'ViewA'. Returning to 'ViewA' restores its previous state.
  3. Final Answer:

    'ViewA' state is preserved because it is cached -> Option B
  4. Quick Check:

    Keep-alive caches dynamic components [OK]
Hint: Switching views inside keep-alive preserves previous state [OK]
Common Mistakes:
  • Assuming components reset on every switch
  • Thinking keep-alive destroys inactive components
  • Believing keep-alive causes runtime errors with dynamic components
4. What is wrong with this code snippet?
<keep-alive>
  <MyComponent :is="currentView" />
</keep-alive>

Assuming currentView is a string with component name.
medium
A. keep-alive cannot wrap components
B. Missing closing tag for keep-alive
C. currentView should be a component object, not a string
D. Using :is on a component tag instead of <component>

Solution

  1. Step 1: Identify correct usage of dynamic components

    Dynamic components require the <component :is="..." /> syntax, not <MyComponent :is="..." />.
  2. Step 2: Check the code snippet

    The snippet incorrectly uses :is on a named component tag, which is invalid and causes errors.
  3. Final Answer:

    Using :is on a component tag instead of <component> -> Option D
  4. Quick Check:

    Dynamic components need <component :is> syntax [OK]
Hint: Use for dynamic components, not named tags [OK]
Common Mistakes:
  • Placing :is on a named component tag
  • Forgetting to close keep-alive tags
  • Thinking keep-alive cannot wrap components
5. You want to cache only the 'UserProfile' component but not 'UserSettings' when switching views. Which is the correct way to use <keep-alive> with the include prop to achieve this?
hard
A. <keep-alive include={UserProfile}><component :is="currentView" /></keep-alive>
B. <keep-alive exclude="UserProfile"><component :is="currentView" /></keep-alive>
C. <keep-alive include="UserProfile"><component :is="currentView" /></keep-alive>
D. <keep-alive include="UserSettings"><component :is="currentView" /></keep-alive>

Solution

  1. Step 1: Understand the include prop on keep-alive

    The include prop accepts a string or array of component names to cache only those components.
  2. Step 2: Apply include to cache 'UserProfile' only

    Setting include="UserProfile" caches only the 'UserProfile' component and excludes others like 'UserSettings'.
  3. Final Answer:

    <keep-alive include="UserProfile"><component :is="currentView" /></keep-alive> -> Option C
  4. Quick Check:

    Use include="UserProfile" to cache only that component [OK]
Hint: Use include prop with component name to cache selectively [OK]
Common Mistakes:
  • Using exclude instead of include to cache desired component
  • Passing include as an object instead of string
  • Including wrong component name in include prop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard