State Management · Junior · Question 4 of 75

What's the right way to update one field of an object in state?

const [form, setForm] = useState({ name: "", email: "" });

Explanation

Unlike this.setState in classes, the useState setter replaces the value entirely - setForm({ email }) would lose name. Mutating and passing the same object is skipped, because React compares by reference (Object.is).

Further reading

Problem with a question

State Management · Junior · question 4

Test yourself

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

Take this quiz