The key attribute helps Vue identify which items in a list have changed, been added, or removed. It improves rendering performance and keeps component state consistent.
Click to reveal answer
beginner
Why should keys be unique in a list?
Keys must be unique so Vue can correctly track each element. If keys are not unique, Vue might mix up elements, causing bugs or incorrect updates.
Click to reveal answer
intermediate
What can happen if you use the index of an array as a key in Vue?
Using the index as a key can cause problems when the list changes order or items are added/removed. Vue may reuse elements incorrectly, leading to UI glitches or wrong component states.
Click to reveal answer
intermediate
How does the key attribute affect component state in Vue?
The key attribute tells Vue when to reuse or recreate components. Changing keys forces Vue to recreate components, resetting their state. This helps avoid stale or wrong data in components.
Click to reveal answer
beginner
When rendering a list of items in Vue, what is the best practice for choosing a key?
Use a unique and stable identifier from the data, like an ID. Avoid using array indexes or values that can change, to keep rendering efficient and correct.
Click to reveal answer
What does the key attribute help Vue do when rendering lists?
ASort the list alphabetically
BChange the color of list items
CTrack which items changed, added, or removed
DHide duplicate items
✗ Incorrect
The key attribute helps Vue track changes in lists for efficient updates.
Why should keys be unique in Vue list rendering?
ATo make the list look colorful
BTo avoid mixing up elements and bugs
CTo speed up network requests
DTo increase font size
✗ Incorrect
Unique keys help Vue correctly identify each element and avoid bugs.
What is a potential problem of using array index as a key?
AVue may reuse elements incorrectly when list changes
BIt makes the list load slower
CIt causes syntax errors
DIt disables animations
✗ Incorrect
Using index as key can cause Vue to reuse elements wrongly if the list order changes.
What happens if you change the key of a component in Vue?
AVue recreates the component and resets its state
BVue changes the component's color
CVue hides the component
DVue duplicates the component
✗ Incorrect
Changing the key forces Vue to recreate the component, resetting its internal state.
What is the best choice for a key in a Vue list?
AThe array index
BThe item’s color
CA random number
DA unique and stable ID from the data
✗ Incorrect
A unique and stable ID ensures Vue tracks elements correctly.
Explain why the key attribute is important when rendering lists in Vue.
Think about how Vue updates the screen efficiently.
You got /4 concepts.
Describe the risks of using array indexes as keys in Vue list rendering.
Consider what happens when items are added or removed.
You got /4 concepts.
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
Step 1: Understand Vue's list rendering
Vue uses the key attribute to identify each item uniquely when rendering lists.
Step 2: Recognize the role of key
This unique key helps Vue efficiently update only changed items instead of re-rendering the whole list.
Final Answer:
It helps Vue track each item uniquely to update the list efficiently. -> Option A
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
Step 1: Check the key binding syntax
The key attribute should be bound with : to a unique and stable value, like item.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 D
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?
Vue uses the key attribute to match existing DOM elements with new data by id.
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 A
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
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.
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 B
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
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 C
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 unknownstrongwhile earning +1 Grip toward your certificate.