Skip to content

createActor

Callable


  • Creates a new actor instance for the given actor logic with the provided options, if any.

    @remarks

    When you create an actor from actor logic via createActor(logic), you implicitly create an actor system where the created actor is the root actor. Any actors spawned from this root actor and its descendants are part of that actor system.

    @example
    import { createActor } from 'xstate';
    import { someActorLogic } from './someActorLogic.ts';

    // Creating the actor, which implicitly creates an actor system with itself as the root actor
    const actor = createActor(someActorLogic);

    actor.subscribe((snapshot) => {
    console.log(snapshot);
    });

    // Actors must be started by calling `actor.start()`, which will also start the actor system.
    actor.start();

    // Actors can receive events
    actor.send({ type: 'someEvent' });

    // You can stop root actors by calling `actor.stop()`, which will also stop the actor system and all actors in that system.
    actor.stop();

    Type parameters

    Parameters

    Returns Actor<TLogic>