0
0
Vueframework~20 mins

Key attribute and why it matters in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when keys are missing in a Vue list rendering?

Consider a Vue component rendering a list of items without using key attributes. What is the most likely behavior when the list changes?

Vue
<template>
  <ul>
    <li v-for="item in items">{{ item.text }}</li>
  </ul>
</template>

<script setup>
import { ref } from 'vue'
const items = ref([{ id: 1, text: 'A' }, { id: 2, text: 'B' }])
</script>
AVue will automatically assign unique keys internally, so no issues occur.
BVue will throw a runtime error about missing keys.
CVue will reuse DOM elements incorrectly, causing unexpected UI updates.
DVue will render the list twice to ensure correctness.
Attempts:
2 left
💡 Hint

Think about how Vue tracks elements in a list to update efficiently.

state_output
intermediate
2:00remaining
What is the output when keys are reused in Vue list rendering?

Given a Vue component rendering a list with duplicate keys, what will be the output after updating the list?

Vue
<template>
  <ul>
    <li v-for="(item, index) in items" :key="item.id">{{ item.text }}</li>
  </ul>
</template>

<script setup>
import { ref } from 'vue'
const items = ref([
  { id: 1, text: 'First' },
  { id: 1, text: 'Duplicate' }
])
</script>
AVue will only render one item because keys must be unique.
BVue will crash with a runtime error.
CVue will throw a warning but render both items anyway.
DVue will render both items correctly with their texts.
Attempts:
2 left
💡 Hint

Keys must be unique to help Vue track elements properly.

📝 Syntax
advanced
2:00remaining
Which option correctly uses the key attribute in Vue 3?

Identify the correct syntax for using the key attribute in a Vue 3 v-for loop.

Vue
<template>
  <ul>
    <li v-for="item in items" ???>{{ item.name }}</li>
  </ul>
</template>
A:key="item.id"
Bv-key="item.id"
Ckey="item.id"
Dv-bind:key="item.id"
Attempts:
2 left
💡 Hint

Remember how to bind dynamic attributes in Vue templates.

🔧 Debug
advanced
2:00remaining
Why does this Vue list update cause unexpected UI behavior?

Examine the code below. After removing the first item from items, the UI does not update as expected. What is the cause?

Vue
<template>
  <ul>
    <li v-for="item in items" :key="item.index">{{ item.text }}</li>
  </ul>
</template>

<script setup>
import { ref } from 'vue'
const items = ref([
  { index: 0, text: 'One' },
  { index: 1, text: 'Two' },
  { index: 2, text: 'Three' }
])

function removeFirst() {
  items.value.shift()
}
</script>
AThe shift method does not modify the reactive array properly.
BUsing array index as key causes Vue to reuse DOM nodes incorrectly after removal.
CThe key attribute is missing, so Vue cannot track items.
DVue requires a unique string key, but numbers are used here.
Attempts:
2 left
💡 Hint

Think about what happens when keys are based on array positions and the array changes.

🧠 Conceptual
expert
3:00remaining
Why is the key attribute critical for Vue's virtual DOM diffing?

Explain why Vue requires a unique key attribute on elements rendered with v-for. What role does it play in Vue's virtual DOM update process?

AKeys are used to style elements uniquely with CSS in Vue components.
BKeys allow Vue to cache component instances permanently, avoiding re-renders.
CKeys prevent Vue from rendering elements multiple times in the DOM.
DKeys help Vue identify which elements have changed, moved, or been removed, enabling efficient DOM updates.
Attempts:
2 left
💡 Hint

Think about how Vue compares old and new virtual DOM trees.