npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

redux-statechart

v1.2.0

Published

redux + xstate = awesome !

Readme

redux-statechart

redux + xstate = awesome !

Redux is a predictable state container for JavaScript apps. One of the redux principles is that your app state is stored in a single tree. Accordingly, UI variables (isLoading, isVisible) and side effects (data fetching response) are all states. When the operation of your app get complicated (it's usually happens quickly), the state always become unpredictable.

Xstate is functional, stateless JavaScript finite state machines (FSM) and statecharts. An FSM is defined by a list of its states, its initial state, and the conditions for each transition. A statechart is a state machine where each state in the state machine may define its own subordinate state machines, called substates. Those states can again define substates. These are useful for declaratively describing the behavior of your app, from the individual components to the overall app logic.

Deterministic state not only reduce time fixing unpredictable and exploded state but also provide a common language for designers & developers. On the other hand, uncertain or extended state are regular when handling side effect in the real world app. Generally speaking, both infinite and finite states are necessary.

Redux do not strictly limit state shape but xstate have to define a machine configuration. Redux-statechart is a tiny library make redux state possessed with both finite and infinite states in the underlying redux pattern.

Usage Example

First, create a statechart machine. Please note that xstate must be version 4.x.

const starWarsMachine = Machine({
  id: "starWars",
  initial: "idle",
  states: {
    idle: {
      on: {
        REQUEST: {
          target: "pending",
          actions: ["alertStartingFirstRequest"]
        }
      },
      onExit: "alertMayTheForceBeWithYou"
    },
    pending: {
      on: {
        SUCCESS: "fulfilled",
        FAILURE: "rejected"
      },
      onEntry: "fetchPerson",
      onExit: "alertRequestFinished"
    }
  }
});

Redux-statechart take array of statechart machiness as the only argument and return a higher order reducer (HOR) and an action creator. HOR takes a regular reducer as the first argument and an optional initial state as the second. Note the initial state must be object.

import RS from "redux-statechart";

// reducerEnhancer is a Higher Order Reducer.
const { machineActionCreator, reducerEnhancer } = RS([starWarsMachine]);

const extReducer = (state, action) => {
  switch (action.type) {
    case "INCREMENT":
      return { ...state, counter: state.counter + 1 };
    case "DECREMENT":
      return { ...state, counter: state.counter - 1 };
    default:
      return state;
  }
};
const enhancedReducer = reducerEnhancer(extReducer, { counter: 0 });

const store = createStore(enhancedReducer);

The principle of reducer do not restrict the shape of state. A reducer is hard to distinguish between finite and infinite state.

In the convention of redux-statechart, the finite state are appended with Mach.

Finally, whether finite or infinite state, call dispatch to update the state. For convenience, redux-statechart contains another returned value, machineActionCreator. It takes machine id and machine event as required arguments and return an action you could dispatch it.

store.dispatch(machineActionCreator("starWars", "REQUEST"));
// => State{ counter: 0, starWarsMach: { value: "pending", ... }}

store.dispatch({ type: "INCREMENT" });
// => State{ counter: 1, starWarsMach: { value: "pending", ... }}

Issues

Feel free to submit issues and enhancement requests.

License

MIT