0
0
Vueframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Keep-alive Master
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 3?

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

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

<script setup>
import { ref } from 'vue'
import ExpensiveComponent from './ExpensiveComponent.vue'
const show = ref(true)
</script>
AThe component is destroyed immediately when toggled off and recreated fresh when toggled on.
BThe component is cached and its state is preserved when toggled off and on again.
CThe component's template is cached but its data and state are reset every time.
DThe component is never rendered because <keep-alive> disables rendering.
Attempts:
2 left
💡 Hint

Think about what does to component instances in Vue.

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

In Vue 3, if a component wrapped in is hidden (v-if false) and then shown again, which lifecycle hooks run?

Vue
<script setup>
import { onMounted, onUnmounted, onActivated, onDeactivated } from 'vue'

onMounted(() => console.log('mounted'))
onUnmounted(() => console.log('unmounted'))
onActivated(() => console.log('activated'))
onDeactivated(() => console.log('deactivated'))
</script>
Amounted, unmounted, activated
Bmounted, unmounted, mounted
Cmounted, deactivated, activated, unmounted
Dmounted, deactivated, activated
Attempts:
2 left
💡 Hint

Remember that caches the component instead of destroying it.

📝 Syntax
advanced
2:00remaining
Which option correctly uses with dynamic components and includes an include prop?

Choose the correct syntax to cache only components named 'ExpensiveComponent' and 'HeavyWidget' using with dynamic components.

Vue
<template>
  <keep-alive :include="['ExpensiveComponent', 'HeavyWidget']">
    <component :is="currentComponent" />
  </keep-alive>
</template>
A
&lt;keep-alive :include="['ExpensiveComponent', 'HeavyWidget']"&gt;
  &lt;component :is="currentComponent" /&gt;
&lt;/keep-alive&gt;
B
&lt;keep-alive include="['ExpensiveComponent', 'HeavyWidget']"&gt;
  &lt;component :is="currentComponent" /&gt;
&lt;/keep-alive&gt;
C
&lt;keep-alive include={ExpensiveComponent, HeavyWidget}&gt;
  &lt;component :is="currentComponent" /&gt;
&lt;/keep-alive&gt;
D
&lt;keep-alive include="ExpensiveComponent,HeavyWidget"&gt;
  &lt;component :is="currentComponent" /&gt;
&lt;/keep-alive&gt;
Attempts:
2 left
💡 Hint

Check how to bind props dynamically in Vue templates.

🔧 Debug
advanced
2:00remaining
Why does the component inside lose its state after toggling?

A developer wraps a component with but notices the component's data resets every time it is toggled off and on. What is the most likely cause?

Vue
<template>
  <keep-alive>
    <ExpensiveComponent v-if="show" :key="uniqueKey" />
  </keep-alive>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)
const uniqueKey = ref(1)
</script>
AThe component is missing the <keep-alive> include prop to enable caching.
BThe <keep-alive> tag does not cache components with v-if directives.
CThe :key changes on toggle, forcing Vue to recreate the component instead of reusing it.
DThe component's data is reset because it uses reactive refs inside setup.
Attempts:
2 left
💡 Hint

Think about how Vue uses keys to identify components.

🧠 Conceptual
expert
3:00remaining
How does improve performance in Vue apps with expensive components?

Explain the main reason why using around expensive components can improve app performance.

A<keep-alive> caches component instances to avoid costly re-creation and re-rendering when toggled.
B<keep-alive> delays rendering of components until they are visible on screen.
C<keep-alive> compresses component data to reduce memory usage during runtime.
D<keep-alive> automatically splits component code into smaller chunks for faster loading.
Attempts:
2 left
💡 Hint

Think about what happens when a component is removed and added back without caching.