React hooks and utilities for integrating XState finite state machines and statecharts into React applications
npx @tessl/cli install tessl/npm-xstate--react@6.0.0@xstate/react provides React-specific utilities and hooks for integrating XState finite state machines and statecharts into React applications. It offers comprehensive state management capabilities with automatic re-rendering, TypeScript integration, and SSR compatibility.
npm install xstate @xstate/reactNote: XState is a peer dependency and must be installed separately.
import { useActor, useActorRef, useSelector, createActorContext, shallowEqual } from "@xstate/react";For CommonJS:
const { useActor, useActorRef, useSelector, createActorContext, shallowEqual } = require("@xstate/react");import { useActor } from "@xstate/react";
import { createMachine } from "xstate";
const toggleMachine = createMachine({
id: "toggle",
initial: "inactive",
states: {
inactive: { on: { TOGGLE: "active" } },
active: { on: { TOGGLE: "inactive" } }
}
});
function Toggle() {
const [state, send] = useActor(toggleMachine);
return (
<button onClick={() => send({ type: "TOGGLE" })}>
{state.value === "inactive" ? "Click to activate" : "Active! Click to deactivate"}
</button>
);
}@xstate/react is built around several key components:
useActor, useActorRef, useSelector) that manage XState actors within React's lifecyclecreateActorContext for sharing actors across component trees via React ContextuseSyncExternalStore for optimal re-renderingPrimary hooks for managing XState actors and subscribing to state changes with automatic React integration.
function useActor<TLogic extends AnyActorLogic>(
logic: TLogic,
options?: ActorOptions<TLogic>
): [SnapshotFrom<TLogic>, Actor<TLogic>['send'], Actor<TLogic>];
function useActorRef<TLogic extends AnyActorLogic>(
machine: TLogic,
options?: ActorOptions<TLogic>,
observerOrListener?: Observer<SnapshotFrom<TLogic>> | ((value: SnapshotFrom<TLogic>) => void)
): Actor<TLogic>;
function useSelector<TActor, T>(
actor: TActor,
selector: (snapshot: TActor extends { getSnapshot(): infer TSnapshot } ? TSnapshot : undefined) => T,
compare?: (a: T, b: T) => boolean
): T;Factory function for creating React context providers and hooks for sharing XState actors across component trees.
interface ActorContextReturn<TLogic extends AnyActorLogic> {
useSelector: <T>(
selector: (snapshot: SnapshotFrom<TLogic>) => T,
compare?: (a: T, b: T) => boolean
) => T;
useActorRef: () => Actor<TLogic>;
Provider: React.ComponentType<{
children: React.ReactNode;
options?: ActorOptions<TLogic>;
logic?: TLogic;
}>;
}
function createActorContext<TLogic extends AnyActorLogic>(
actorLogic: TLogic,
actorOptions?: ActorOptions<TLogic>
): ActorContextReturn<TLogic>;Helper functions for common operations and compatibility.
function shallowEqual(objA: any, objB: any): boolean;
// Deprecated - use useActor instead
function useMachine<TMachine extends AnyStateMachine>(
machine: TMachine,
options?: ActorOptions<TMachine>
): [StateFrom<TMachine>, Actor<TMachine>['send'], Actor<TMachine>];The following types are commonly used throughout the @xstate/react API:
// From XState - these types are re-exported or used in signatures
type AnyActorLogic = any; // XState's actor logic type
type Actor<TLogic> = any; // XState's actor reference type
type SnapshotFrom<TLogic> = any; // Extracted snapshot type from logic
type ActorOptions<TLogic> = any; // XState's actor configuration options
type Observer<T> = any; // XState's observer interface
type AnyStateMachine = any; // XState's state machine type
type StateFrom<TMachine> = any; // Extracted state type from machine
// React integration types
interface ReactNode {} // React's node type for children
interface ComponentType<P> {} // React's component typeNote: These types are defined by XState and React. Refer to their respective documentation for complete type definitions.