Stable List Identity
Part of the Fundamentals section of Coddy's Vue journey. Lesson 24 of 55.
Keys connect rendered rows to data identities. If a list can reorder, use a stable id rather than the position. An unbound input then follows its record when the record moves.
const books = ref([{id: 10, title: 'Dune'}, {id: 20, title: 'Emma'}]);
// <li v-for="book in books" :key="book.id">
// {{ book.title }} <input aria-label="Note">
// </li>Without stable identity, Vue may reuse a row at the same position for a different record. The label changes but an unbound input can keep the previous record's text. A unique id stays with the record across sorting and deletion; a new random key would destroy that continuity.
Challenge
MediumRender the supplied people as li elements in ul#result. Each row shows the name and an unbound input with id note- followed by the person id. Button #reverse reverses the array. Use stable keys so each person keeps their own typed note after reordering.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks