0
0
Vueframework~20 mins

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

Choose your learning style9 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 .