Frontend Blocks typescript Intermediate

Multi-step Form with State Machine

Wizard form with XState state machine for step transitions and validation guards.

typescript
import { createMachine, assign } from "xstate";

const formMachine = createMachine({
  id: "form",
  initial: "step1",
  context: { email: "", amount: 0 },
  states: {
    step1: {
      on: {
        NEXT: {
          target: "step2",
          guard: ({ context }) => context.email.includes("@"),
          actions: assign({ email: (_, e) => e.email }),
        },
      },
    },
    step2: {
      on: {
        SUBMIT: "submitting",
        BACK:   "step1",
      },
    },
    submitting: { type: "final" },
  },
});

Prerequisites

  • xstate >= 5
  • React >= 17

Pro Tips

  • → Visualize the machine at stately.ai/viz before coding.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
typescript
Level
Intermediate
Read time
5 min
Published
May 2026
License
Commercial License

Newsletter

New modules every week, directly to your inbox.