Stately
XState v6 alpha

Input and output

Pass input to actors and read output when they complete.

XState v6 is in alpha

APIs and behavior may change before the stable release.

Input provides data when an actor is created.

const greetingMachine = createMachine({
  context: ({ input }: { input: { name: string } }) => ({ name: input.name })
});

const greeting = createActor(greetingMachine, {
  input: { name: 'Ada' }
}).start();

Final output

const machine = createMachine({
  context: { result: 42 },
  initial: 'working',
  states: {
    working: { on: { finish: { target: 'done' } } },
    done: { type: 'final' }
  },
  output: ({ context }) => ({ result: context.result })
});

Read output from a completed snapshot or an invocation's done event.

Input configures one actor instance. Output reports its final result.

  • An invoice actor can receive a currency and line items as input, then output the calculated total.
  • A file-processing actor can receive a file key as input, then output the generated asset URL.

Actors that run indefinitely may never produce output.

TypeScript

Declare input and output schemas when they cross actor boundaries.

Input and output cheatsheet

context: ({ input }) => ({ value: input.value })
output: ({ context }) => ({ value: context.value })
createActor(machine, { input: { value: 1 } });

On this page