Understanding Keys
Keys help React identify which items have changed, been added, or removed. They're crucial for performance and correctness when rendering lists.
Why Keys Matter
Keys give elements a stable identity:
Loading React playground...
Good vs Bad Keys
Loading React playground...
When Index Keys Are OK
Index as key is acceptable in specific cases:
Loading React playground...
Generating Unique Keys
Loading React playground...
Exercise: Fix the Keys
Loading exercise...
Exercise: Composite Keys
Loading exercise...
Exercise: Key Validation
Loading exercise...
Key Takeaways
- Keys identify items - Help React track changes efficiently
- Use unique IDs - Database IDs or unique properties are best
- Avoid index as key - Only for static, append-only lists
- Composite keys - Combine properties when no single unique value
- Keys must be unique - Among siblings, not globally

