Modeling State Machines
You'll learn: How reducers naturally express finite state machines, preventing impossible state combinations like being in loading and error states simultaneously.
The Problem with Boolean Flags
When you model state with multiple boolean flags (isLoading, hasError, isSuccess), you create many impossible combinations. Your component could theoretically be in 8 states (2³), but only 3-4 are valid. This leads to defensive checks everywhere and bugs when invalid combinations slip through.
Reducers as State Machines
A state machine defines:
- A finite set of possible states (not state values—states as in "modes")
- Valid transitions between those states
Reducers naturally express this pattern. Instead of multiple booleans, you store a single status value:
function reducer(state, action) {
switch (state.status) {
case 'idle':
if (action.type === 'FETCH_START') {
return { status: 'loading', data: null, error: null };
}
break;
case 'loading':
if (action.type === 'FETCH_SUCCESS') {
return { status: 'success', data: action.payload, error: null };
}
if (action.type === 'FETCH_ERROR') {
return { status: 'error', data: null, error: action.error };
}
break;
// ... other states
}
return state; // Ignore invalid transitions
}
The Benefit: Impossible States Become Unrepresentable
With status: 'loading', you cannot simultaneously have status: 'error'. The state machine structure makes invalid combinations literally impossible to construct. You can't accidentally set isLoading = true and forget to clear hasError.
Your rendering logic becomes cleaner too—one switch on state.status instead of nested conditions checking multiple flags.
Key Takeaway: Reducers let you model state as a finite state machine where each "mode" is explicit and transitions are controlled, eliminating entire classes of bugs caused by impossible state combinations.