React · Middle · Question 2 of 75
How do you reset the state of <EditForm /> when switching to another user?
- 1Reset all fields in a
useEffectwhenuserIdchanges - ✓Correct answer: Pass
key={userId}- React recreates the component - 3Call
forceUpdate()on the form component
Explanation
A new key tells React it's a different component: the old one unmounts along with its state, and a new one mounts. Resetting via an effect works, but first renders stale data and then fixes it - an extra render and flicker. forceUpdate exists only on classes and doesn't reset state.