Performance: Importance of keys
HIGH IMPACT
This concept affects how React efficiently updates the UI by minimizing unnecessary DOM changes during re-renders.
const listItems = items.map(item => <li key={item.id}>{item.name}</li>);const listItems = items.map((item, index) => <li key={index}>{item.name}</li>);| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Index as key | More DOM node replacements | Multiple reflows on reorder | Higher paint cost due to layout thrashing | [X] Bad |
| Unique stable key | Minimal DOM updates | Single reflow for changed items | Lower paint cost with stable layout | [OK] Good |