
vedant
on November 23, 2025
4 min read
so say you've got a shopping cart. you change quantities, hit update total, and a line gets added. you can edit lines, remove them, or empty the cart. that's fine until two of those overlap. you hit update total, edit a line before the total comes back, and now the total on screen doesn't match the items.
see the full state machines deck
a state machine is a way to stop that. you write down the situations the cart can be in, like idle, updating or editing, and then which actions are allowed in each one. if a move isn't on the list, it can't happen.
i compared two versions of the same cart UI. one used a state machine, through a tool called xstate. it has five actions (quantity change, update total, edit, remove and empty) and all the numbers live in one place. an update can't overlap an edit, so the total always matches the lines.
the other kept its data in a few places, with bits of code that run whenever something changes. nothing stopped you editing while an update was still going, so you'd get totals that didn't match and saves that only half finished. not cos anyone was careless. nothing in the code said those situations weren't allowed.
the state machine one is easier to read, almost like a flowchart, and adding a new action is easy. the other one was more work to change and more work to debug.
i wouldn't use one for every little toggle. but when something has real sequences, like this cart, it's worth it. for the person using it, it just means the number on screen is the right one.
so say you've got a shopping cart. people change quantities, they hit update total, a line gets added, they edit a line, they remove one, they empty the cart. fine, until two of those overlap. someone hits update total, then edits a line before the new total comes back, and now the number on screen doesn't match the items. that's the bug, and not a rare one.
see the full state machines deck
a state machine is how you stop that. you write down the situations the cart can actually be in. idle, updating total, editing, whatever you need. then you write down which actions are allowed in each situation, and what happens when they fire. if a move isn't on the list, it doesn't happen. you don't get updating-and-editing-at-once unless you put that on the list on purpose.
that's the whole sell. not a framework thing. just: here are the states, here are the events, here are the only transitions you're allowed.
i compared two versions of the same cart UI. one used a library called XState, which is a state machine you write in code. five event handlers, that's it.
quantity change, updates the preview as you type
update total, computes the total and settles the line
edit a line
remove a line
empty, wipes the cart
all the actual data lives in one place. the quantities, the lines, the total. transitions are written down, so an update can't overlap an edit. related updates happen together, so you don't save a total that doesn't match the lines.
the other version kept state in a few places, and little bits of code that run whenever something changes. nothing says you can't edit a line while an update is still going. that's how you get mismatched totals, a cart that doesn't match what's on screen, and the fun class of bugs where two clicks in the wrong order save garbage.
two things happening at once, a save that only half-finished, a state the UI was never meant to show. not cos the people who wrote it were careless. cos nothing in the code made those states illegal.
the XState one is easier to read. you can stare at it like a flowchart. adding a new action is adding a transition, not hunting for the bit of code that runs on the side. totals feel simpler cos the machine queues the work instead of letting it overlap.
the other one is more work to change and more work to debug. that's the maintenance cost, not a philosophy thing.
I'm not saying put a state machine on every toggle. a bit of local UI state is fine. this cart has real sequences, add, edit, remove, empty, live preview, and it was a mess without a machine. that's when I'd bother.
Sign in to join the conversation