Use keyed v-for
key with v-for is always required on components, in order to maintain internal component state down the subtree. Even for elements though, it's a good practice to maintain predictable behavior, such as object constancy in animations.
Let's say you have a list of todos:
js
Don't Do ❌data() {
return {
todos: [
{
id: 1,
text: 'Learn to use v-for'
},
{
id: 2,
text: 'Learn to use key'
}
]
}
}
template
Do ✔️<ul>
<li v-for="todo in todos">
{{ todo.text }}
</li>
</ul>
template
<ul>
<li
v-for="todo in todos"
:key="todo.id"
>
{{ todo.text }}
</li>
</ul>
OrangeHRM Dev Docs