Performance: Object vs Multiple Variables
What you'll learn: You'll discover that using one object or multiple useState calls performs identically in terms of re-renders.
The Performance Question
Many developers worry: "If I call setState three times, do I get three re-renders? Should I use one object instead?"
Here's the good news: React batches state updates in event handlers and lifecycle methods. Whether you call three separate setters or update one object, you get one re-render per batch.
How Batching Works
When you update state inside an event handler:
function handleClick() {
setFirstName('John');
setLastName('Doe');
setAge(30);
// ✅ ONE re-render, not three
}
React queues all these updates and performs a single re-render at the end. The same happens with an object:
function handleClick() {
setUser({
...user,
firstName: 'John',
lastName: 'Doe',
age: 30
});
// ✅ Also ONE re-render
}
The Real Performance Factors
The performance difference isn't about how many re-renders, but:
- Readability: Multiple variables are often clearer
- Update logic: Spreading large objects can be verbose
- Independence: Separate states don't force unrelated re-checks
Think of it like ordering at a restaurant: whether you order three items separately or as a combo meal, the kitchen still prepares everything at once.
Key Takeaway: Both approaches trigger one re-render per update batch—choose based on logic and clarity, not performance myths.