Stately
XState v6 alpha

Share actors

Share one actor through Solid context.

XState v6 is in alpha

APIs and behavior may change before the stable release.

Create an actor reference in a parent and provide it with Solid context.

const ActorContext = createContext<ActorRefFrom<typeof machine>>();

function ActorProvider(props: ParentProps) {
  const actorRef = useActorRef(machine);
  return (
    <ActorContext.Provider value={actorRef}>
      {props.children}
    </ActorContext.Provider>
  );
}

Descendants call useContext(ActorContext) to use the same actor.

This works well for a cart shared by checkout components or a session actor shared by one application shell. Create one actor per application or request.