react-key-prop

Guide proper key prop usage in React lists with stable data IDs.

5|Updated Oct 17, 2011
One-click install
npx skills add https://github.com/klen/dotfiles --skill react-key-prop-klen
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: react-key-prop
Source: https://github.com/klen/dotfiles/tree/main/.config/opencode/skills/react-key-prop
Command: npx skills add https://github.com/klen/dotfiles --skill react-key-prop-klen

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses common pitfalls and best practices when using the key prop in React lists, ensuring efficient and bug-free rendering of dynamic data.

Core Features & Use Cases

  • Stable Key Generation: Guides on using unique, stable IDs from data for React keys.
  • Index Anti-Pattern Avoidance: Explains why and when not to use array indices as keys, especially for dynamic lists.
  • Use Case: When mapping an array of user objects to a list of <li> elements, this skill helps you choose the correct key prop (e.g., user.id) to prevent state bugs and performance issues during updates.

Quick Start

Use the react-key-prop skill to understand how to properly assign a key to list items when mapping over an array of data.

Frequently Asked Questions about react-key-prop

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does my React list lose component state when items are reordered or deleted?

React list rendering loses state when using unstable keys like array indices. Assigning stable, unique data IDs to the key prop ensures React correctly tracks elements across reorders, preventing state-related bugs and UI inconsistencies.

When should I avoid using array index as the key prop in React lists?

Avoid using array index as the key prop in React lists when the list is dynamic and items can be inserted, deleted, or reordered. Indices cause incorrect component matching, leading to state bugs; use stable data IDs instead.

How do I generate stable keys for React list items mapped from an array?

To generate stable keys for React list items, use unique identifiers from your data objects, such as user.id. This ensures stable UI rendering and efficient updates when mapping arrays to components like li elements.

What is the best way to troubleshoot list-related state inconsistencies in React?

The best way to troubleshoot list-related state inconsistencies in React is to audit your key props. Replacing array indices with stable, unique data IDs resolves mapping mismatches and restores correct component state management.

Can I use array index as a key prop for static React lists?

Using array index as a key prop is acceptable for static React lists where items are never reordered or mutated. For dynamic lists, stable data IDs are required to prevent state bugs and maintain rendering performance.