Dev.Lib
arrow_backBack to Blogs
LEVEL_044.6June 27, 20261 min read

Finite state modeling

State & Data Flow

Model UI as explicit states and events (idle, loading, success, error) — avoid boolean soup (`isLoading && !isError && ...`).

Summary

Model UI as explicit states and events (idle, loading, success, error) — avoid boolean soup (isLoading && !isError && ...). XState/state machines prevent impossible states. Forms, wizards, and modals benefit most. Illegal transitions indicate design bugs early.

Code example

const fetchMachine = {
  idle: { FETCH: 'loading' },
  loading: { SUCCESS: 'success', ERROR: 'error' },
  success: { FETCH: 'loading' },
  error: { FETCH: 'loading', RETRY: 'loading' },
};

function transition(state, event) {
  return fetchMachine[state]?.[event] ?? state;
}

Key points

  • Model UI as explicit states and events (idle, loading, success, error) — avoid boolean soup (isLoading && !isError && ...).
  • XState/state machines prevent impossible states.
  • Forms, wizards, and modals benefit most.
  • Illegal transitions indicate design bugs early.

Further reading

Related concepts