{"success":true,"name":"give-react-list-items-stable-keys-from-their-ids-never-the-a","description":"Rendering a React list that can be filtered, sorted, or edited","category":"ai-brimind","path":"ai-brimind/give-react-list-items-stable-keys-from-their-ids-never-the-a/SKILL.md","file":"SKILL.md","content":"---\nname: give-react-list-items-stable-keys-from-their-ids-never-the-a\ndescription: \"Rendering a React list that can be filtered, sorted, or edited\"\n---\n\n# Give React List Items Stable Keys From Their IDs — Never the Array Index When Lists Change\n\n## When to use\nYou are writing a .map() render in React where items can be added, removed, reordered, or filtered, and the key is the array index or missing.\n\n## Example request\nA filterable member list keyed by index → deleting the first row makes every input below show the WRONG member's data; React matched old and new rows by position, and position lied. Keys from item.id (the DB _id) → React tracks each row through any reorder; the key is the row's identity card, and identity is exactly what the database ID already is. A truly static, never-changing list → index keys are technically harmless there; the trap only springs when the list CHANGES, which is why it hides in demos and bites in production. Two lists on one page reusing the same ids → keys only need uniqueness among siblings; no global scheme needed.\n\n## Instructions\nAct as a React developer who knows what the key prop actually is — React's only way to recognize 'this rendered row IS that data item from last render' when the list changes — and that keying by array index tells React 'position equals identity', a statement that becomes a lie the instant anything is inserted, deleted, sorted, or filtered: React then reuses the component (and its internal state — checkbox ticks, input drafts, animations) for a DIFFERENT item, producing the classic ghost bug where deleting row one makes row two inherit row one's half-typed text. For every list render, do this: (1) Key by the item's stable identity: the database _id/id it already carries — your data almost always has one, and it is precisely what a key should be. If a list genuinely lacks IDs (raw strings from user input), create stable ones at creation time (a generated id stored with the item), not at render time — Math.random() or Date.now() IN the render is worse than index, as it forces full remount of every row on every render. (2) Reserve index keys for the one honest case and mark it: a list that is never reordered, never filtered, never has items inserted or removed except at the end — and say in a comment why index is safe here, so the next feature ('add sorting!') knows the assumption it is breaking. (3) Keep keys where React needs them — on the outermost element returned by the .map(), including on Fragments (<Fragment key=...>) — and remember uniqueness is required only among siblings, so two separate lists may reuse the same ids freely. (4) Read the console like a colleague: React's 'each child should have a unique key' warning is not lint noise to silence with index — it is React telling you it cannot track your rows; fix it with identity, not with whatever stops the yellow text. (5) Verify with the state-preservation test: give rows some internal state (tick a checkbox, type in a row's input), then delete an earlier item or re-sort — the state must travel WITH its item, not stay at its position. Only call the list done when filtering and deleting move data and component-state together as one unit — a list that passes only when nobody touches it is keyed by luck, and luck is keyed by index.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #236\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}