advanced · lesson 8 of 8

State for UX

State machines, optimistic updates, undo.

Sidhanshu Monga·1 min·
You’ll be able to
  1. Model an interaction as explicit states and transitions
  2. Make impossible states impossible
  3. Implement optimistic updates and undo safely

Many interaction bugs come from state that can't exist in the real world: loading and error at the same time, a success message for a request that's still pending. Modelling state explicitly removes whole classes of these bugs and makes every state visible to design.

From booleans to states

Three booleans give eight combinations, most of them meaningless. A single status lists only the real ones:

type Status =
  | { kind: 'idle' }
  | { kind: 'loading' }
  | { kind: 'success'; data: Item[] }
  | { kind: 'error'; message: string };

Now the error message only exists in the error state, and the data only in success.

State machines

A state machine adds the events that move between states and rejects the ones that don't make sense: a "submit" event is ignored while already submitting. Statecharts, introduced by David Harel in 1987, extend this with nested and parallel states; libraries such as XState implement them. Even without a library, drawing the states and arrows is the fastest way to find missing designs.

Optimistic updates

Apply the expected result immediately, remember the previous state, and reconcile when the server responds. On failure, roll back and tell the user. See Optimistic UI.

Undo

Undo is a pending state between "requested" and "done". Keep the removed item and its position until the window closes, then commit:

trust
Undo vs confirm
A confirm dialog asks for courage up front and trains people to click through it. Undo lets them act at full speed and recover afterwards.
Motion tokens — final draft
Design team
Your deployment is ready
Vercel
Re: focus ring audit
Accessibility guild
#destructive actions#recovery
Confirm first, or act now and undo after.
Try it

Pick a component with several boolean flags (isLoading, isError, isOpen…). Draw its real states and the events that move between them. Rewrite it with a single status value and check which "impossible" combinations disappeared.

Key takeaways
01One explicit status beats several booleans
02Every state needs a design, including the unhappy ones
03Optimism and undo are state problems before they're UI problems