State & ArchitectureBaseline · Widely

Finite State Machines for UI

Mental Model

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.

Interactive Simulator

State Modeling: 4 Booleans (16 States) vs FSM (4 States)

Compare error-prone boolean flags against an explicit discriminated-union transition table.

Lines of Code
Before
4
After
5
Reflows / Browser Cost
Before
2^4 = 16 implicit combinations
After
4 valid states only
BeforeBoolean Soup (Impossible States Allowed)
if (isLoading && !isError && isSuccess) {
  // How did we get here? Forgot to reset isSuccess on retry.
}
setIsLoading(true); // forgot setIsError(false)!
Lines: 4Reflows: 2^4 = 16 implicit combinationsDependencies: Defensive if/else guards everywhere

Spinner and error toast can render at the same time; double-clicks fire duplicate requests.

After (Native)Finite State Machine (Explicit Transitions)
const transitions = {
  idle: { FETCH: 'loading' },
  loading: { RESOLVE: 'success', REJECT: 'error' },
  error: { RETRY: 'loading' }
};
Lines: 5Reflows: 4 valid states onlyDependencies: Zero external runtime required

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.

  1. Finite states: Enumerate every legal mode (idle | loading | success | error) as a tagged union.
  2. Events: Represent triggers as explicit objects ({ type: 'SUBMIT' }) rather than ad-hoc setter calls.
  3. Transition function: A pure transition(state, event) checks the current state first, ignoring any event not valid for that state.
TS
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 stored isLoading/isError booleans inside the state object.
  • 2018+ (XState & Tagged Unions): Brought strict TypeScript discriminated unions and W3C SCXML statecharts to frontend workflows.

Production Pitfalls

Production Pitfall
  • 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 AbortController when leaving loading -> idle must happen deterministically on transition, or stale responses still land.
When Not to Use
  • 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

Useful Prerequisites
Foundational node
State & ArchitectureFinite State Machines for UI

Seen in Architecture Autopsies

Mechanical Comparisons