How do keys affect performance and correctness beyond lists?
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
- Why does a component re-render, and is re-rendering always bad?
- What does React.memo do?
- What is the difference between useMemo and useCallback?
- What is the React Compiler, and does it replace useMemo and useCallback?
- How do you lazy-load components in React?
- How do you render a very long list efficiently?