Skip to content

Instantly share code, notes, and snippets.

@TheJaredWilcurt
Created February 3, 2019 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheJaredWilcurt/cbd42480cc8a3a3ead1556c1c0aaec0c to your computer and use it in GitHub Desktop.
Save TheJaredWilcurt/cbd42480cc8a3a3ead1556c1c0aaec0c to your computer and use it in GitHub Desktop.
v-for keys best practice

If you just list the index number then the keys are not unique:

<template>
  <!-- keys will be 0, 1, 2... -->
  <div v-for="(row, rowIndex) in rows" :key="rowIndex">
    <!-- keys will be 0, 1, 2... -->
    <div v-for="(cell, cellIndex) in row.cells" :key="cellIndex">
    </div>
  </div>
</template>

Instead, if you follow this pattern, it is always safes:

<template>
  <!-- keys will be row0, row1, row2... -->
  <div v-for="(row, rowIndex) in rows" :key="'row' + rowIndex">
    <!-- keys will be row0cell0, row0cell1, row1cell0, row1cell1, row2cell0... -->
    <div v-for="(cell, cellIndex) in row.cells" :key="'row' + rowIndex + 'cell' + cellIndex">
    </div>
  </div>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment