Bird
Raised Fist0
Vueframework~5 mins

Key attribute and why it matters in Vue

Choose your learning style10 modes available
Introduction

The key attribute helps Vue track elements when lists change. It makes updates faster and avoids bugs.

When rendering a list of items with <code>v-for</code>.
When items in a list can be added, removed, or reordered.
When you want Vue to reuse elements correctly instead of recreating them.
When you want to keep input fields or components from losing focus or state during updates.
Syntax
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.

Examples
Using a unique id as the key for each user.
Vue
<template>
  <ul>
    <li v-for="user in users" :key="user.id">{{ user.name }}</li>
  </ul>
</template>
Using the index as key is not recommended because it can cause bugs when the list changes.
Vue
<template>
  <ul>
    <li v-for="(item, index) in items" :key="index">{{ item }}</li>
  </ul>
</template>
Sample Program

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.

Vue
<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>
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. Why is the key attribute important when rendering lists in Vue?
easy
A. It helps Vue track each item uniquely to update the list efficiently.
B. It changes the style of list items automatically.
C. It disables reactivity for list items.
D. It sorts the list items alphabetically.

Solution

  1. Step 1: Understand Vue's list rendering

    Vue uses the key attribute to identify each item uniquely when rendering lists.
  2. Step 2: Recognize the role of key

    This unique key helps Vue efficiently update only changed items instead of re-rendering the whole list.
  3. Final Answer:

    It helps Vue track each item uniquely to update the list efficiently. -> Option A
  4. Quick Check:

    Unique keys improve list update efficiency = A [OK]
Hint: Keys uniquely identify list items for efficient updates [OK]
Common Mistakes:
  • Thinking keys change styles automatically
  • Believing keys disable reactivity
  • Assuming keys sort the list
2. Which of the following is the correct way to use the key attribute in a Vue v-for loop?
easy
A. <li v-for="item in items" :key="index">{{ item }}</li>
B. <li v-for="item in items" :key="items">{{ item }}</li>
C. <li v-for="item in items" key="item">{{ item }}</li>
D. <li v-for="item in items" :key="item.id">{{ item.name }}</li>

Solution

  1. Step 1: Check the key binding syntax

    The key attribute should be bound with : to a unique and stable value, like item.id.
  2. 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.
  3. Final Answer:

    <li v-for="item in items" :key="item.id">{{ item.name }}</li> -> Option D
  4. Quick Check:

    Use :key with unique id = D [OK]
Hint: Use :key with a unique item property, not index [OK]
Common Mistakes:
  • Using index as key which can cause bugs
  • Not binding key with : causing string literal
  • Using the whole array as key
3. Given the code below, what will be the rendered list after the 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>
medium
A.
  • Banana
  • Cherry
  • Date
B.
  • Apple
  • Banana
  • Cherry
C.
  • Date
  • Banana
  • Cherry
D.
  • Banana
  • Date
  • Cherry

Solution

  1. Step 1: Understand the key-based update

    Vue uses the key attribute to match existing DOM elements with new data by id.
  2. 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.
  3. Final Answer:

    <ul><li>Banana</li><li>Cherry</li><li>Date</li></ul> -> Option A
  4. Quick Check:

    Keys track items by id, so list updates correctly = C [OK]
Hint: Keys keep items matched by id, so order updates correctly [OK]
Common Mistakes:
  • Assuming old items stay without keys
  • Thinking new items replace all old items
  • Ignoring the order change in the array
4. What is the problem with this Vue code snippet?

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

Assuming items can be reordered or changed dynamically.
medium
A. The key attribute is missing a colon for binding.
B. Using index as key can cause incorrect DOM updates when list changes.
C. The v-for syntax is invalid without parentheses.
D. The item.name interpolation is incorrect.

Solution

  1. Step 1: Identify the key usage problem

    Using the loop index as key is unstable when the list changes order or items are added/removed.
  2. 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.
  3. Final Answer:

    Using index as key can cause incorrect DOM updates when list changes. -> Option B
  4. Quick Check:

    Index keys cause update bugs = B [OK]
Hint: Never use index as key if list can reorder [OK]
Common Mistakes:
  • Thinking index keys are always safe
  • Missing colon on key attribute
  • Confusing v-for syntax errors
5. You have a list of users where some users share the same name but have unique IDs. You want to render this list with Vue's v-for. Which key choice is best to avoid rendering bugs and improve performance?
hard
A. Use the user's index in the array as the key.
B. Use the user's name as the key.
C. Use the user's unique ID as the key.
D. Do not use any key attribute.

Solution

  1. Step 1: Analyze uniqueness requirement for keys

    Keys must be unique and stable for each item to help Vue track them correctly.
  2. 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.
  3. Final Answer:

    Use the user's unique ID as the key. -> Option C
  4. Quick Check:

    Unique stable ID keys prevent bugs = A [OK]
Hint: Always pick unique stable IDs as keys, never names or indexes [OK]
Common Mistakes:
  • Using non-unique keys like names
  • Using index keys causing bugs on reorder
  • Skipping keys causing slow updates
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