The Cognitive Load of Many useState Calls
You'll learn why components with six or more useState declarations become difficult to understand and maintain.
The Problem: Too Many Moving Parts
When you declare state with useState, each piece of state is independent. One or two state variables? No problem. But when your component grows to have six, eight, or ten separate useState calls, something changes:
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState(null);
const [retryCount, setRetryCount] = useState(0);
const [lastSubmittedAt, setLastSubmittedAt] = useState(null);
const [validationErrors, setValidationErrors] = useState([]);
const [isDirty, setIsDirty] = useState(false);
Your brain has to track eight different setters, remember which state variable controls what, and understand how they interact. It's like trying to juggle while riding a unicycle.
Why This Matters
Cognitive load is the mental effort required to understand code. Each useState call adds:
- One more variable to track
- One more setter to remember
- One more piece of the puzzle when debugging
When state values are related (like isSubmitting, error, and retryCount all dealing with form submission), scattering them across multiple declarations hides their relationship.
The Consolidation Solution
useReducer offers a way out: it consolidates all related state into a single source of truth. Instead of eight separate pieces, you have one state object and one dispatch function. The relationships between state values become explicit in your reducer logic.
Think of it like organizing loose papers (multiple useState calls) into a single binder with labeled sections (useReducer).
Key Takeaway: Six or more useState declarations create cognitive overload; useReducer reduces this by consolidating related state into one manageable structure with clear update patterns.