Finite State Machines for UI
Four boolean flags (isLoading, isError, isSuccess, isEmpty) create $2^4 = 16$ possible combinations, 12 of which are bugs. A Finite State Machine collapses them into one explicit status enum and a (state, event) -> nextState table so impossible UI states cannot exist.
State Modeling: 4 Booleans (16 States) vs FSM (4 States)
Compare error-prone boolean flags against an explicit discriminated-union transition table.
if (isLoading && !isError && isSuccess) {
// How did we get here? Forgot to reset isSuccess on retry.
}
setIsLoading(true); // forgot setIsError(false)!Spinner and error toast can render at the same time; double-clicks fire duplicate requests.
const transitions = {
idle: { FETCH: 'loading' },
loading: { RESOLVE: 'success', REJECT: 'error' },
error: { RETRY: 'loading' }
};FETCH in 'loading' state is structurally ignored—zero double-submits.
Under the Hood
Boolean flags act like four light switches wired to one bulb: nothing stops code from flipping isLoading and isError on together. A Finite State Machine works like a rotary dial, only one position is active, blocking Optimistic UI races and Stale Closures.
- Finite states: Enumerate every legal mode (
idle | loading | success | error) as a tagged union. - Events: Represent triggers as explicit objects (
{ type: 'SUBMIT' }) rather than ad-hoc setter calls. - Transition function: A pure
transition(state, event)checks the current state first, ignoring any event not valid for that state.
type State = { status: 'idle' } | { status: 'loading' } | { status: 'error'; message: string };
type Event = { type: 'FETCH' } | { type: 'FAIL'; message: string } | { type: 'RESET' };
function transition(state: State, event: Event): State {
if (state.status === 'idle' && event.type === 'FETCH') return { status: 'loading' };
if (state.status === 'loading' && event.type === 'FAIL') return { status: 'error', message: event.message };
if (state.status === 'error' && event.type === 'RESET') return { status: 'idle' };
return state; // Invalid event for current state is safely ignored
}Problem Genealogy
- 1987 (David Harel Statecharts): Formalized hierarchical and parallel state machines for avionics and embedded UIs.
- 2015 (Redux Reducers): Popularized
(state, action) => nextState, though teams still storedisLoading/isErrorbooleans inside the state object. - 2018+ (XState & Tagged Unions): Brought strict TypeScript discriminated unions and W3C SCXML statecharts to frontend workflows.
Production Pitfalls
- Event explosion on simple toggles: Writing a 60-line statechart configuration for an open/closed disclosure panel adds ceremony with zero safety gain.
- Forgetting cleanup on state exit: Cancelling an in-flight
AbortControllerwhen leavingloading->idlemust happen deterministically on transition, or stale responses still land.
- When not to use an FSM: For continuous numeric values (scroll offset, canvas coordinates) or simple independent form fields, plain state or reactive signals are far lighter.
Connections & Logical Next Steps
Hierarchical statecharts extend flat (state, event) transition tables with nested compound states and orthogonal regions.
Modeling pending, confirmed, and rolled-back phases as finite states prevents double-mutations.
Pure (state, event) => nextState reducers evaluate transitions cleanly without closing over stale flags.
Signals propagate arbitrary reactive value changes, whereas finite state machines enforce explicit transition guards.
Seen in Architecture Autopsies
Mechanical Comparisons
Signals propagate arbitrary reactive value changes, whereas finite state machines enforce explicit transition guards.
Pure (state, event) => nextState reducers evaluate transitions cleanly without closing over stale flags.