Member-only story
How To Handle Actor Output onDone Events in XState v5+ with React
I love that state machines make React a dream programming tool for managing frontend app lifecycle in a deterministic way. But, while XStateās actor model is a perfect fit with its state machine architecture, actor āoutputā events (onDone) are confusing AF.
I love XState (especially XState v5 with TypeScript) for building my apps, because otherwise React feels like a pile of useEffect race conditions.
Sure, the UI is guaranteed to update, but side effects across multiple componentsā lifecycles do not run in a guaranteed order.
That can result in all sorts of weird bugs, from showing empty data to logged-out users or admin routes to logged-out users all the way to memory leaks and fully crashing your production app. Fun stuff!
XState is way more complicated than simply jerry-rigging a few thousand lines of complex application logic in useEffect, I admit that.
But using XState guarantees that your app is in a given state, not just Reactās promise that your appās components will react to changes in state.

