Course contentsShow
← React
Lesson 349 of 2,39610. useState BasicsFree lesson

State Updates Don't Merge

Learn that setState replaces the entire state value—no automatic object merging like class components.

State Updates Don't Merge

You'll learn: How useState replaces the entire state value instead of merging like older class components did.

The Core Difference

When you call a state setter from useState, React replaces the old state value with the new one completely. This is different from class components, where this.setState() automatically merged new properties into the existing state object.

With useState, if your state is an object and you only update one property, you must manually spread the old values or you'll lose them.

The Replace Behavior

const [user, setUser] = useState({ name: 'Alice', age: 25 });

// ❌ This REPLACES the entire object - age is lost!
setUser({ name: 'Bob' });
// user is now { name: 'Bob' } — no age property

// ✅ Manually spread to preserve other properties
setUser({ ...user, name: 'Bob' });
// user is now { name: 'Bob', age: 25 }

Why It Works This Way

Class components had one big state object, so merging made sense. With hooks, you typically split state into multiple useState calls for different values. If you need an object, you're responsible for the merge.

Analogy

Think of it like replacing a photo in a frame. Class component setState was like Photoshop: it would edit just the parts you specified. useState setters are like swapping the entire photo: if you hand over a new picture with only half the scene, that's all you get.

Key Takeaway: useState setters replace the entire state value—if your state is an object, spread the old properties yourself to preserve them: setState({ ...oldState, newProp: value }).