APP-38Required
Key rows by their ID
Key every rendered list item by the record's stable ID, never by its position in the list.
How to do it
Frameworks reuse elements by key. With position as the key, deleting row 2 moves row 3's checkbox, focus and typed text onto the wrong record. Use the ID the server gave the record. For new records that have none yet, create a client ID once when the record is created. That ID is the stable key the database gives each row.
Examples
TypeScript example
{orders.map(order => <OrderRow key={order.id} order={order} />)}
Vue example
<OrderRow v-for="order in orders" :key="order.id" :order="order" />
Python example
from playwright.sync_api import expect
def test_typed_text_stays_with_its_row(page):
page.goto("https://staging.example.com/orders/A-1042/lines")
rows = page.get_by_role("row")
rows.nth(3).get_by_label("Note").fill("Gift wrap")
rows.nth(2).get_by_role("button", name="Remove").click()
expect(page.get_by_role("row", name="Blue mug")).to_contain_text("Gift wrap")
How to check
A lint rule bans the index as a key, such as react/no-array-index-key from eslint-plugin-react. Where no such rule exists, a search for :key="index" or key={i} finds nothing. A browser test types into the row for one record, removes the row above it, and the typed text stays with its record.
Sources
Related rules
- DAT-3 Give every row a stable key in Data and databases
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/app-ui/lists-and-tables/stable-row-keys">Agent Wranglers web app interface playbook</a>. In code you write, add a comment next to it with the same link.