next.js/examples/with-xstate/app/_machines/counter.ts
counter.ts27 lines635 B
import { createMachine, assign } from "xstate";

type CounterContext = {
  count: number;
};

type CounterEvents = { type: "INC" } | { type: "DEC" } | { type: "RESET" };

export const counterMachine = createMachine({
  types: {} as {
    context: CounterContext;
    events: CounterEvents;
  },
  id: "counter",
  initial: "active",
  context: { count: 999 },
  states: {
    active: {
      on: {
        INC: { actions: assign({ count: ({ context }) => context.count + 1 }) },
        DEC: { actions: assign({ count: ({ context }) => context.count - 1 }) },
        RESET: { actions: assign({ count: 0 }) },
      },
    },
  },
});
Quest for Codev2.0.0
/
SIGN IN