Q6: Why are keys important when rendering lists in React?
Keys are unique string identifiers used by React to track which list items have changed, been added, or been removed. Proper keys enable efficient Reconciliation during Virtual DOM diffing. Using array indices as keys is discouraged for dynamic lists as it can lead to state bugs during reordering.
Key Takeaways
- Keys help React identify elements uniquely during list diffing.
- Stable, unique keys prevent incorrect state mapping during list reordering or deletions.
Knowledge Check
Why is using array index as a key for dynamic lists considered bad practice?