Skip to content

Quick Start

Terminal window
npm install matchina

Here’s a simple traffic light state machine that demonstrates Matchina’s core concepts:

import { defineStates, matchina } from "matchina";
const states = defineStates({
Red: () => ({ message: "Stop" }),
Yellow: () => ({ message: "Prepare to stop" }),
Green: () => ({ message: "Go" }),
});
export const createTrafficLightMachine = () => {
return matchina(
states,
{
Red: { next: "Green" },
Yellow: { next: "Red" },
Green: { next: "Yellow" },
},
"Red"
);
};
export type TrafficLightMachine = ReturnType<typeof createTrafficLightMachine>;

For a detailed walkthrough and explanation, see the Traffic Light Example.

  • State Definition: Define states with typed data using defineStates()
  • Machine Creation: Create a state machine with matchina() by specifying states, transitions, and initial state
  • Transition Definition: Define allowed state transitions as a simple object structure
  • React Integration: Integrate state machines in React components with useMachine()