The key attribute helps Vue track elements when lists change. It makes updates faster and avoids bugs.
Key attribute and why it matters in Vue
<template>
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
</template>The key must be unique for each item in the list.
Use a stable identifier like an ID, not the array index.
id as the key for each user.<template>
<ul>
<li v-for="user in users" :key="user.id">{{ user.name }}</li>
</ul>
</template><template>
<ul>
<li v-for="(item, index) in items" :key="index">{{ item }}</li>
</ul>
</template>This component shows a list of fruits. Clicking the button shuffles the list. Using :key="item.id" helps Vue update the list efficiently without losing track of each fruit.
<template>
<div>
<button @click="shuffle">Shuffle List</button>
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
</div>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' }
])
function shuffle() {
items.value = [...items.value].sort(() => Math.random() - 0.5)
}
</script>Always use a unique and stable key to avoid rendering bugs.
Keys help Vue know which elements changed, so it can update only those.
Using array index as key can cause problems when items are reordered or removed.
The key attribute is important for list rendering in Vue.
It must be unique and stable for each item.
Using keys improves performance and prevents bugs when lists change.
Practice
key attribute important when rendering lists in Vue?Solution
Step 1: Understand Vue's list rendering
Vue uses thekeyattribute to identify each item uniquely when rendering lists.Step 2: Recognize the role of
This unique key helps Vue efficiently update only changed items instead of re-rendering the whole list.keyFinal Answer:
It helps Vue track each item uniquely to update the list efficiently. -> Option AQuick Check:
Unique keys improve list update efficiency = A [OK]
- Thinking keys change styles automatically
- Believing keys disable reactivity
- Assuming keys sort the list
key attribute in a Vue v-for loop?Solution
Step 1: Check the key binding syntax
Thekeyattribute should be bound with:to a unique and stable value, likeitem.id.Step 2: Identify the correct unique key usage
<li v-for="item in items" :key="item.id">{{ item.name }}</li>uses:key="item.id", which is correct and stable for each item.Final Answer:
<li v-for="item in items" :key="item.id">{{ item.name }}</li> -> Option DQuick Check:
Use :key with unique id = D [OK]
- Using index as key which can cause bugs
- Not binding key with : causing string literal
- Using the whole array as key
items array changes?<template>
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' }
])
// Later update
items.value = [
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' },
{ id: 4, name: 'Date' }
]
</script>Solution
Step 1: Understand the key-based update
Vue uses thekeyattribute to match existing DOM elements with new data byid.Step 2: Match items by id and update list
Items with id 2 and 3 remain, item with id 1 is removed, and item with id 4 is added at the end.Final Answer:
<ul><li>Banana</li><li>Cherry</li><li>Date</li></ul> -> Option AQuick Check:
Keys track items by id, so list updates correctly = C [OK]
- Assuming old items stay without keys
- Thinking new items replace all old items
- Ignoring the order change in the array
<ul>
<li v-for="(item, index) in items" :key="index">{{ item.name }}</li>
</ul>Assuming
items can be reordered or changed dynamically.Solution
Step 1: Identify the key usage problem
Using the loop index askeyis unstable when the list changes order or items are added/removed.Step 2: Understand the impact on DOM updates
This causes Vue to reuse DOM elements incorrectly, leading to bugs like wrong item content or lost input focus.Final Answer:
Using index as key can cause incorrect DOM updates when list changes. -> Option BQuick Check:
Index keys cause update bugs = B [OK]
- Thinking index keys are always safe
- Missing colon on key attribute
- Confusing v-for syntax errors
v-for. Which key choice is best to avoid rendering bugs and improve performance?Solution
Step 1: Analyze uniqueness requirement for keys
Keys must be unique and stable for each item to help Vue track them correctly.Step 2: Evaluate each option
Using the unique ID is best because it is unique and stable. Names are not unique if duplicated. Index is unstable if the list changes. No key causes inefficient updates.Final Answer:
Use the user's unique ID as the key. -> Option CQuick Check:
Unique stable ID keys prevent bugs = A [OK]
- Using non-unique keys like names
- Using index keys causing bugs on reorder
- Skipping keys causing slow updates
Ready to prove it?
Pass the test and this pattern moves unknown
