Skip to content

vue-v-for-key ​

A v-for without a :key, keyed by its position in a row that keeps state, or keyed by a new object or array.

Why: when a list changes, Vue matches the new rows to the old ones by key. Without a key, or with the index as the key, it matches them by position: delete the first row, and the second row's data moves into the first row's element, along with the first row's state. The text typed into an input, the focus, an open <details>, a child component's data all end up next to the wrong item. A key built as a new object (:key="{ id }") never matches the previous render, so every row is thrown away and rebuilt each time. Guide: Vue.

✕ Rejectedvue-v-for-key
vue
<script setup lang="ts">
import GuestCard from "./GuestCard.vue";

type Guest = { readonly id: string; readonly name: string };

const props = defineProps<{ guests: readonly Guest[] }>();
</script>

<template>
  <ul>
    <li v-for="(guest, index) in props.guests" :key="index">
      <input :value="guest.name" />
    </li>
  </ul>
  <GuestCard v-for="guest in props.guests" :guest="guest" />
</template>
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import GuestCard from "./GuestCard.vue";

type Guest = { readonly id: string; readonly name: string };

const props = defineProps<{ guests: readonly Guest[] }>();
</script>

<template>
  <ul>
    <li v-for="guest in props.guests" :key="guest.id">
      <input :value="guest.name" />
    </li>
  </ul>
  <GuestCard v-for="guest in props.guests" :key="guest.id" :guest="guest" />
</template>

A list without ids can still have a stable key: build it from what each item holds (:key="`${line.date}-${line.title}`", or the ids of what a row groups), as long as two items never share it. Any key from the item is fine; only the position is reported.

A position key is only reported where it can do harm: rows that hold a component, a form field (input, textarea, select, details, video, audio), a v-model or a template ref, at any depth. A list of plain text keyed by index is fine. The index is recognized as the third name of (value, key, index), or a second name like i, idx, index or rowIndex.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra