React · Middle · Question 65 of 75

Why might a list of controlled inputs lose focus on each keystroke?

{fields.map((f) => <input key={Math.random()} value={f.value} onChange={...} />)}

Explanation

Keys must be stable. A changing key destroys the old DOM node (and its focus and state) and creates a new one.

Further reading

Problem with a question

React · Middle · question 65

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