DEV Community

Discussion on: "Just Use Props": An opinionated guide to React and XState

Collapse
 
mattpocockuk profile image
Matt Pocock

No - state is not shared between the two machines.

This is like calling useReducer twice, using the same reducer.

Collapse
 
redbar0n profile image
Magne • Edited

Ah, ok, I see that useMachine actually creates a new service...

Is there another way of having the state independent of the React tree (so one does not even have to pass props)? (While also having React re-render the components where the state is used when it changes).

Maybe putting the const [state, send] = useMachine(machine); into a shared hook that is used inside the relevant components?

Or extract that line outside of any component and then referencing state and send inside components (as a closure)?

Thread Thread
 
redbar0n profile image
Magne

Seems like using React's Context is the recommended approach: github.com/statelyai/xstate/discus...

Thread Thread
 
redbar0n profile image
Magne

This is the closest to what I was originally thinking: github.com/statelyai/xstate/discus...