How do keys affect performance and correctness beyond lists?

React performance · Experienced

Model answer

Changing a component's key tells React to unmount the old instance and mount a new one, which resets its state. That is a clean way to reset a form when the selected item changes: <Profile key={userId} />.

Bad keys in lists (index or random values) cause unnecessary remounts or state attached to the wrong item.

Learn this topic

React Performance: memo, useMemo, useCallback and When Not to Use Them in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions