Service not initialised when passed to React.Context #1256
-
Or is it my implementation of it? I’ve created a small sandbox to replicate it: https://codesandbox.io/s/usingcontext-k0iqd. It doesn’t make much sense passing this machine in context, but I refitted a previously created machine. The problem seems to be that the service isn’t initialised on mount. Because ‘manually’ starting it right after the useMachine call makes it all work (uncomment it in src/SubscriptionProvider). It doesn’t sit right starting it (again and again?). Is this some caveat I should be aware of? I’ve got a use case where I’d like to expose a set of events to different places in the UI (using the service context hook), and have the machine hook extended with services responding to transitions (used “internally” ie. not in the UI). Also! Thank you for a wonderful library! 💅 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
A quick workaround is to add a conditional existence check before rendering. This will probably be solved in the upcoming |
Beta Was this translation helpful? Give feedback.
A quick workaround is to add a conditional existence check before rendering.
This will probably be solved in the upcoming
@xstate/react
v1.0, as the service is started immediately, or at least will have its state available immediately. It's a bit tricky to determine what to do here, since React has no notion of "initial effects", as the initial render is supposed to be without any effects, andservice.start()
is technically an effect.