React Lists & Keys
Lists and Keys are essential concepts when rendering multiple elements dynamically from an array.
A list in React is a way to display multiple similar items (like text, images, or components) by looping over an array using .map(). It allows dynamic and scalable rendering of UI elements.
React Keys
Keys help React identify which items have changed, are added, or are removed. This improves performance and avoids bugs during re-rendering.
Why are keys important?
- They help React maintain the correct order and identity of elements.
- Without keys, React re-renders all items, even if only one changes.
important
- Avoid using array indexes as keys if the list can change (reorder, delete, etc.).
- Prefer using a unique id if available.
Concept | Purpose |
---|---|
List | Dynamically render elements from an array |
Key | Give each element a unique identity to optimize updates |