React · Middle · Question 2 of 75

How do you reset the state of <EditForm /> when switching to another user?

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.

Further reading

Problem with a question

React · Middle · question 2

Test yourself

Take the React · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz