Sub topic 1
Where useState starts to strain
useState is the right tool for most state. This chapter shows the specific situations where it becomes hard to maintain. Those situations are the reason useReducer exists.
Prerequisites
This chapter assumes you already know:
- JavaScript functions, objects, arrays, and the spread operator.
- React function components and JSX.
- The
useStatehook and how a state update triggers a re-render. - Props and basic event handling in React.
What useState does well
With one value, or a few unrelated values, useState is hard to beat. A toggle, a text input, or a selected tab each need one variable and one setter. The update logic is a single expression, so it fits inside the event handler.
The strain starts when state has several parts that must change together, or when many handlers change the same state in different ways.
Strain 1: related values drift apart
Here is a cart that keeps items and total in two separate state variables.
import { useState } from 'react';
export default function Cart() {
const [items, setItems] = useState([]);
const [total, setTotal] = useState(0);
function addItem(name, price) {
setItems([...items, { name, price }]);
setTotal(total + price);
}
function removeItem(name) {
setItems(items.filter(item => item.name !== name));
// Bug: total is never reduced.
}
return (
<>
<button onClick={() => addItem('Pen', 2)}>Add pen</button>
<button onClick={() => removeItem('Pen')}>Remove pen</button>
<p>{items.length} items, total ${total}</p>
</>
);
}After you add and then remove a pen, the cart is empty but the total still reads $2. Nothing in React prevents this. Every handler must remember to update both variables, and the compiler cannot tell you when one forgets.
The cleaner model is to store only items and compute the total during render. That works here. It does not help when the related values cannot be derived from each other, such as a status string alongside an error message.
Strain 2: update logic is spread across handlers
In a larger component, each handler contains its own piece of the rules. To answer "what are all the ways the cart can change?", you have to read every handler. Adding a new rule, such as "adding a duplicate item raises its quantity", means finding and editing several places.
The rules live inside the event handlers, mixed with UI code. That makes them hard to read as a group and hard to test, because you must render the component to exercise them.
Strain 3: updates depend on the current state
When the next value depends on the previous one, you need the functional form of the setter to avoid stale reads.
setCount(c => c + 1);This works for one value. When an update touches several fields, each with its own rule, the updater functions grow long. The nesting of { ...state, nested: { ...state.nested, ... } } also gets harder to review.
A way to see the pattern
The three problems share one cause: the rules for changing state are scattered across the component. The fix is to put them in one place and have handlers only describe what happened.
In the reducer version, handlers no longer know how state changes. They report an event, and one function decides the result. Later chapters build this up step by step: the reducer function, the actions it receives, and the useReducer hook that connects them to a component.
Signs you are hitting the limit
Look for these in your own code:
- Two or more state variables that must always change together.
- Several handlers that each call the same group of setters.
- A bug fix that required editing three handlers.
- State shaped as a nested object that you update with long spread chains.
- Update rules you would like to test without rendering anything.
One or two of these does not mean you must switch. A later chapter covers how to weigh useState against useReducer. For now, the goal is to recognize the symptoms.
Exercise
A component keeps isLoading, data, and error in three separate useState calls. Name one bug this setup allows, and say why the setup allows it.
Show answer
Nothing forces the three values to change together, so a handler can leave them inconsistent. For example, a failed request might call setError(...) but forget setIsLoading(false), leaving the UI showing both a spinner and an error. Each setter is independent, so React cannot detect a combination that should not exist.