@xstate/fsm vs @xstate/react vs @xstate/test
State Management Libraries Comparison
1 Year
@xstate/fsm@xstate/react@xstate/testSimilar Packages:
What's State Management Libraries?

XState is a powerful library for managing state using finite state machines and statecharts. It provides a structured approach to handling complex state logic, making it easier to visualize and reason about the state of an application. The three packages serve different purposes: '@xstate/fsm' focuses on finite state machines, '@xstate/react' integrates XState with React for seamless state management in React applications, and '@xstate/test' offers utilities for testing state machines, ensuring that state transitions behave as expected. Together, they provide a comprehensive toolkit for implementing and testing state management in web applications.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
@xstate/fsm1,205,85428,42457.1 kB1582 years agoMIT
@xstate/react1,170,91228,42436.2 kB15816 days agoMIT
@xstate/test18,40028,42472.7 kB158-MIT
Feature Comparison: @xstate/fsm vs @xstate/react vs @xstate/test

Integration

  • @xstate/fsm:

    @xstate/fsm is a standalone library that provides a simple API for defining finite state machines. It can be integrated into any JavaScript application, making it versatile for various use cases beyond React.

  • @xstate/react:

    @xstate/react is specifically designed for React applications, offering hooks like useMachine and useService that allow developers to easily integrate state machines into their React components, ensuring a smooth and idiomatic React experience.

  • @xstate/test:

    @xstate/test is a testing utility that is framework-agnostic. It can be used with any JavaScript testing framework, allowing developers to write tests for their state machines regardless of the environment.

Testing Capabilities

  • @xstate/fsm:

    @xstate/fsm does not provide built-in testing utilities, but it allows developers to create state machines that can be tested using any JavaScript testing framework, giving flexibility in testing approaches.

  • @xstate/react:

    @xstate/react does not include direct testing utilities, but it facilitates the testing of stateful components by allowing developers to simulate state changes and observe component behavior during tests.

  • @xstate/test:

    @xstate/test excels in providing a robust framework for testing state machines. It allows developers to define test cases for state transitions and actions, ensuring that the state machine behaves as expected under various conditions.

Learning Curve

  • @xstate/fsm:

    @xstate/fsm has a moderate learning curve, especially for those unfamiliar with the concepts of finite state machines. However, once understood, it provides a clear and structured way to manage state.

  • @xstate/react:

    @xstate/react is relatively easy to learn for developers already familiar with React. The integration of XState concepts into React hooks makes it intuitive for React developers to adopt.

  • @xstate/test:

    @xstate/test is straightforward to use for developers who are already familiar with testing in JavaScript. It provides a clear API for defining tests, making it easy to ensure state machine correctness.

State Visualization

  • @xstate/fsm:

    @xstate/fsm allows for the creation of state machines that can be visualized using tools like XState Visualizer, making it easier to understand and communicate the state logic of an application.

  • @xstate/react:

    @xstate/react leverages the visualization capabilities of XState, allowing developers to visualize state machines defined with @xstate/fsm, enhancing the understanding of state transitions in a React context.

  • @xstate/test:

    @xstate/test does not provide visualization features directly, but it works seamlessly with state machines that can be visualized, ensuring that the tests correspond to the defined state logic.

Extensibility

  • @xstate/fsm:

    @xstate/fsm is highly extensible, allowing developers to create complex state machines with nested states, parallel states, and more, providing the flexibility needed for intricate applications.

  • @xstate/react:

    @xstate/react extends the capabilities of @xstate/fsm by providing React-specific features like context and services, enabling more complex interactions within React components.

  • @xstate/test:

    @xstate/test is designed to work with any state machine created using @xstate/fsm, allowing developers to test complex state logic and interactions without limitations.

How to Choose: @xstate/fsm vs @xstate/react vs @xstate/test
  • @xstate/fsm:

    Choose '@xstate/fsm' if you need a lightweight solution for implementing finite state machines without any framework dependencies. It is ideal for projects where you want to manage state transitions explicitly and clearly without the overhead of a full state management library.

  • @xstate/react:

    Opt for '@xstate/react' if you are building a React application and want to leverage XState's capabilities within the React ecosystem. This package provides hooks and components that make it easy to integrate state machines into your React components, enhancing state management with a declarative approach.

  • @xstate/test:

    Select '@xstate/test' when you need to ensure the reliability of your state machines through testing. This package is essential for developers who want to write tests for their state machines, verifying that state transitions and actions are executed correctly, thus improving the robustness of the application.

README for @xstate/fsm

@xstate/fsm


XState FSM
XState for Finite State Machines

This package contains a minimal, 1kb implementation of XState for finite state machines.

Features

| | @xstate/fsm | XState | | --------------------------- | :-------------: | :-------------------------------------------: | | Finite states | ✅ | ✅ | | Initial state | ✅ | ✅ | | Transitions (object) | ✅ | ✅ | | Transitions (string target) | ✅ | ✅ | | Delayed transitions | ❌ | ✅ | | Eventless transitions | ❌ | ✅ | | Wildcard transitions | ✅ | ✅ | | Nested states | ❌ | ✅ | | Parallel states | ❌ | ✅ | | History states | ❌ | ✅ | | Final states | ❌ | ✅ | | Context | ✅ | ✅ | | Entry actions | ✅ | ✅ | | Exit actions | ✅ | ✅ | | Transition actions | ✅ | ✅ | | Parameterized actions | ❌ | ✅ | | Transition guards | ✅ | ✅ | | Parameterized guards | ❌ | ✅ | | Spawned actors | ❌ | ✅ | | Invoked actors | ❌ | ✅ |

  • Finite states (non-nested)
  • Initial state
  • Transitions (object or strings)
  • Context
  • Entry actions
  • Exit actions
  • Transition actions
  • state.changed

If you want to use statechart features such as nested states, parallel states, history states, activities, invoked services, delayed transitions, transient transitions, etc. please use XState.

Quick start

Installation

npm i @xstate/fsm

Usage (machine)

import { createMachine } from '@xstate/fsm';

const toggleMachine = createMachine({
  id: 'toggle',
  initial: 'inactive',
  states: {
    inactive: { on: { TOGGLE: 'active' } },
    active: { on: { TOGGLE: 'inactive' } }
  }
});

const { initialState } = toggleMachine;

const toggledState = toggleMachine.transition(initialState, 'TOGGLE');
toggledState.value;
const untoggledState = toggleMachine.transition(toggledState, 'TOGGLE');
untoggledState.value;
// => 'inactive'

Usage (service)

import { createMachine, interpret } from '@xstate/fsm';

const toggleMachine = createMachine({});

const toggleService = interpret(toggleMachine).start();

toggleService.subscribe((state) => {
  console.log(state.value);
});

toggleService.send('TOGGLE');
toggleService.send('TOGGLE');
toggleService.stop();