This article has become part of the official XState docs!
Many React applications follow the Flux architecture popularised by Redux. This setup ...
For further actions, you may consider blocking this person and/or reporting abuse
Thank you for this. Really love it. Question for you, from what I see this is impossible to inject later some services like you will do with a simple useMachine when it's local machine right ? What I mean by that it's I want to use it as global like you do but would like the services to be inject in the component where those thing happen. But I don't think it's possible.
Could you provide some code to explain what you mean? What are you looking to achieve?
Something like you do with useMachine
But doing it won't work cause this look like create a new instance
What would you expect to happen in this instance? I am still confused by what you're trying to achieve.
You're correct, the above code won't work.
What I try to do it's in my case the service are apollo mutation. I don't want to upfront all the mutation need in this global service. By doing this way I would be able to control the pattern from the component but getting it to work with the global machine.
Why do you want the apollo mutation to be held in the global service? It feels more natural to me to have it in the component where it's used.
I want my service who is global for all the onboarding part to handle the logic and make component quite simple.
Right - I think that's a mistake. Instead, you should keep truly local state local. I would make a state machine inside the component to handle this.
Cause I was trying to implement like you show in the xstate catalog with the multi steps form. Was working well until this issue. Thank you
The snippet that shows how to use
useActor
may be outdated:This is with react@17.0.2, xstate@4.23.0 and @xstate/react@npm:1.5.1.
The issue is that you're not consuming the context provider.
github.com/statelyai/xstate/issues...
That looks really nice! I have been using state machines with react for a few weeks now too and I'm in love with them.
I really liked your implementation of the Context and I didn't know about the useSelector, so thanks a lot for the help :)
Thanks Fernando!
state
inconst selector = (state) => {
isn't typed. What type we should use on it? TS is complaining about it. Also I don't have type support in thereturn state.matches('loggedIn');
};
matches
method. How to apply one?For anyone having TypeScript issues with this in XState 4.29+ (where there are new
schema
andtsTypes
attributes on the machine, see xstate.js.org/docs/guides/typescri...),ActorRefFrom
no longer seems to work but you can useInterpreterFrom
in its placeLooks like it's fixed now:
github.com/statelyai/xstate/commit...
My main struggle with XState is global state management. I like creating the services in a parent machine and pulling them out from context but this example is definitely clean and easy to follow. Thanks for sharing.
Xstate both looks and sounds phenomenal! 🤩 Thanks a mil for this