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: "" });- ✓Correct answer:
setForm({ ...form, email: value }) - 2
form.email = value; setForm(form) - 3
setForm({ email: value })- React merges the objects itself
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).