What problem does it solve?
This skill helps developers ensure stable identity for list items in React by teaching when to use data IDs versus array indices, how to generate IDs, and how to avoid common key-related performance pitfalls in reconciliations.
Core Features & Use Cases
- Use Data IDs (Preferred): Always use unique, stable identifiers directly from your data to serve as the key.
- Generate IDs on Data Load: Create IDs once when data is loaded if items lack IDs (e.g., attach _tempId: nanoid()).
- When Index Is Acceptable (Rare): Only use index as key when the list is absolutely static and items have no internal state.
- Anti-Patterns to Avoid: Never generate keys during render; don't use index for dynamic lists; avoid useId() for list keys.
- Quick Real-World Example: Rendering a todo list with each item having a unique id to preserve component state across updates.
Quick Start
Map your data to components using a stable, unique id from each item as the key.