Three Booleans Is Not a State Machine: Modelling UI Logic That Can't Be in Two States at Once
I review a lot of component code. The most common bug pattern I see is not a typo. It is this: const [isLoading, setIsLoading] = useState(false); const [isError, setIsError] = useState(false); const [isSuccess, setIsS
I review a lot of component code. The most common bug pattern I see is not a typo. It is this:
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [isSuccess, setIsSuccess] = useState(false);
Three booleans, eight combinations, three of which are legal. The other five are bugs waiting for a slow network.
Why the bug is structural, not careless
Everyone knows not to set isLoading and isSuccess at the same time. Nobody sets them deliberately. They happen because the flags are set in different places, on different async paths, sometimes in an await that resolves after the component re-rendered.
Add a quantity and a retryCount and the handshake gets worse. Add a "pending but optimistic" case and you have invented a fourth flag, and now four booleans give you sixteen states where five are legal.
This is the classic argument for a discriminator:
type Status =
| { kind: "idle" }
| { kind: "validating" }
| { kind: "submitting"; optimistic: boolean }
| { kind: "error"; message: string; retryable: boolean }
| { kind: "success"; orderId: string };
One variable, five states, no illegal combinations by construction. TypeScript will force you to handle each case. This is a finite state machine, and you do not need a tool to get the benefit.
When you do need a tool
A plain discriminated union is enough for a lot of components. The tool starts earning its place when transitions have conditions that non-engineers need to see and tune.
Consider a product card with these rules:
- hover reveals quick-add, unless the item is out of stock
- quick-add is disabled while
submitting - a failed request retries twice, then shows an error
- success shows a toast and resets after 2.4s
- changing quantity while submitting cancels the previous request
- reduced-motion users skip the success animation but still see the toast
That is not a union. That is a graph, and the people who care most about the exact timing and states are designers, not the person writing the reducer.
How Exode models it
The editor's logic surface is a hierarchical state machine with typed inputs, transitions and listeners.
- Inputs: boolean, number, string, colour. These are the machine's interface to the app.
-
States:
idle,hover,submitting,error,success, with sub-states where needed. - Transitions: guarded by input values and events.
- Listeners: fire an animation, set another input, navigate, or call a webhook.
The compiled component exposes the same interface, so the app drives it with props:
<ExodeUICanvas
artboard="product-card"
inputs={{ quantity, status, outOfStock }}
onTrigger={(name) => {
if (name === "addToCart") handleAdd();
}}
/>
And the motion for each state is part of the same file — the success spring, the error shake, the toast timing. There is no separate "animation spec" document to drift.
The refusal case
A state machine is not automatically better. I have seen statecharts with four states that should have been two CSS classes, and diagrams nobody updates because the code moved on.
Three signs you should not use one:
1. The logic is a pure function of props. isOpen && <Panel /> does not need a machine.
2. Nobody will maintain the diagram. A state machine that drifts from the code is worse than a boolean, because it lies with confidence.
3. The states are mostly visual variants. Hover, focus, pressed and disabled are CSS pseudo-classes. Do not put them in a graph.
The interesting middle is the awkward case: three to eight states, real async, shared with a designer. That is where a visual machine pays for itself.
The five-state rule of thumb
If you can describe a component's behaviour as a sentence — "it is idle, then submitting, then success or error" — write the union and move on.
If describing it takes a paragraph with the word "unless" in it twice, draw the graph.
A migration that worked
I moved a checkout button from booleans to a machine last quarter. The old code had four flags and a useEffect that reset them, and one bug where a fast double-click could set isSuccess and isError in the same tick.
The new version had five states and one status prop. The failure path was a transition, not a flag being set in a catch. The double-click bug became structurally impossible because submitting has one exit and the click handler is ignored inside it.
The diff was smaller than the original component. That is the part people do not expect: a correct state model is usually less code, because you stop writing the guard clauses that patch an incorrect one.
The general principle
State is not a bag of facts. It is a position in a graph. Every time you add a boolean, ask what combinations it makes legal — and whether all of them should be.
Most of the time, one discriminated union is the whole answer. Sometimes you need a visual machine because a designer has to see it. Both are better than isLoading && !isError && isSuccess === false.
UX Baba writes about interaction design and front-end architecture. The state machine surface described here is part of the Exode editor — exodeui.com if you want to draw one.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.