Replies: 1 comment 8 replies
-
|
I've investigated this topic in depth, and from my experience, XState isn't really designed to support re-usability between machines (@davidkpiano correct me if I'm wrong). Trying to abstract shared states across machines quickly becomes a nightmare due to the heavy reliance on TypeScript inference. The generic types get out of hand, easily exceeding 10 required generic parameters to pass down if you want proper typing across different machines with different contexts and events. I also attempted to build utility functions for actions and actors, hoping to inject them into machines for reuse. While it can technically work at runtime, TypeScript constantly complained about mismatched or missing types. Even with strict alignment on context shapes and event types, I found the DX was not worth it, especially when one small divergence between machines required a full reworking of the abstraction. In the end, I abandoned XState for this very reason. I went with my own implementation, less complex with only the features I need. There is also this big caveat that XState (v5) has a single shared context between all the states, making it unsafe to use (paradoxical). I like to call it the Schrodinger context. At this time and state, the value might be there or not. It is too painful to fight the type system. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I wonder if anyone has tried to declare reusable states that are returned by a TS function? I see that on my project I have similar scenarios where for example, from init state I always go to the state that fetch some list from API, asign it to context variable and in onDone it goes to listLoaded state or listLoadError. I have exactly same pattern across 3 machines and my idea was to just create a function that will return such state as it's an object.
After full day of fighting typescript, I find it very difficult to properly type such function. Despite the fact that calling such function in state machine fully works, the typescript cannot calm down for a sec and always see some deep type being invalid.
My example would go like this:
Then in my machines all I want to do is in states property be able to write:
{ loadingList: getLoadingListState() }How to type such function? What I see is that each machine needs whole context interface to be provided, but event with this I'm still fighting with type system,. What do i need to do to make this function generic across 3 different machines assuming context variable name for list or actors names are going to be the same.
I wonder if anyone had similar expirience or maybe has appropriate solution to this.
Beta Was this translation helpful? Give feedback.
All reactions