The Diffing Algorithm
React uses a Virtual DOM to optimize updates. When a list changes, React compares the new elements with the previous ones.
Fig 1.1: Without keys, React re-renders the entire branch. With keys, it simply moves the existing DOM nodes.
Why not use Math.random()?
Using key={Math.random()} is a critical error. Since a new key is generated on every render, React will unmount and remount every list item every single time, destroying state (like input focus or scroll position) and killing performance.