The neighbourhood head carries one lantern between four places on the lane, idle, loading, success, error, and walks only the roads on his map; with three…
Why it matters
Spinners that never go away, flickering errors, “loading AND success at once”: all from tangled booleans. An FSM makes impossible states impossible. It is also exactly the useReducer pattern (state + dispatch).
The key idea
One active state makes impossible states impossible
Common mistakes
The spinner & an error message show up at once, when only one should 3 booleans = 8 combinations, half impossible. Use one state + transitions: impossible states cannot be written.
A clicked event still gets handled though the current state should not accept it An FSM refuses events with no transition from the current state. Without it, you can “resolve” something that never started loading.
Reaching for a statechart library for a flow that is actually simple An object { state: { event: next } } + a reducer is enough for most cases. Reach for XState when you need nested states, guards or effects.
Try it in this playground
idle → FETCH → loading Only FETCH has a road from idle. The neighbourhood head walks to the loading house.
Send RESOLVE while idle Refused: there is no road. The neighbourhood head stays put, no crash.
loading → REJECT → error → RETRY The error path has RETRY (back to loading) and RESET.
Try it with three booleans FETCH, REJECT, FETCH: the spinner and the error message light up together.