Skip to content

Creating state machines with shared context across instances #39

Answered by Lexpeartha
DenniLa2 asked this question in Q&A
Discussion options

You must be logged in to vote

I think what you want to achieve can be done with sharing the same context between state machines. However that would require some modifications:

interface TestContext {
  state: ExState;
}

export const sharedContext: TestContext = {
  state: ExState.initial,
};

And in your component:

const machine1 = useMachine(testMachine, { context: sharedContext });
// These instances should share same state
const machine2 = useMachine(testMachine, { context: sharedContext });

Of course this in itself won't work, since you would have to update state machine to use assign() to update the context, and for that I recommend you check documentation reference

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@DenniLa2
Comment options

@Lexpeartha
Comment options

Answer selected by Lexpeartha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
2 participants