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 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-fsm-reducer

v2.0.0

Published

useReducer meets finite state machines

Downloads

304

Readme

useFsmReducer

Heavily inspired by David Khourshid's article.

useFsmReducer is a hook which helps to bring state machine code quality to a common React pattern - useReducer.

It helps you bootstrap a reducer, similar to redux-toolkit, but also helps extract your effects and help you scale complexity.

Installation

npm i use-fsm-reducer

Example Code

import useFsmReducer from 'use-fsm-reducer';

/**
 * Our state can be in any of these states.
 * Note that data is only declared when we're in
 * the 'loaded' state.
 */
type State =
  | { type: 'pending' }
  | { type: 'errored' }
  | { type: 'loaded'; data: string };

/**
 * We declare all possible actions that can flow
 * through this reducer
 */
type Action =
  | { type: 'reportDataLoaded'; data: string }
  | { type: 'reportError' }
  | { type: 'retry' };

/**
 * We declare all effects that this reducer can fire off.
 *
 * An effect is anything you would put inside a useEffect - an
 * impure function that happens as a result of piece
 * of state changing, or an action firing.
 */
type Effect =
  | { type: 'loadData' }
  | { type: 'showErrorMessage'; message: string };

/**
 * You get back state and dispatch, the API you're
 * used to from useReducer
 */
const [state, dispatch] = useFsmReducer<State, Action, Effect>({
  /**
   * Here, we declare the initial state. Each state
   * must have a 'type'.
   */
  initialState: {
    type: 'pending',
  },
  /**
   * We can run some initial effects on mount.
   */
  runEffectsOnMount: [{ type: 'loadData' }],
  states: {
    /**
     * Each state can handle actions on its own,
     * and fire off effects.
     */
    pending: {
      on: {
        reportDataLoaded: (state, action) => {
          return {
            type: 'loaded',
            data: action.data,
          };
        },
        reportError: () => {
          return {
            type: 'errored',
            /**
             * Return an array of all the effects
             * you'd like this state change to trigger
             */
            effects: [{ type: 'showErrorMessage', message: 'Oh noooo!' }],
          };
        },
      },
    },
    errored: {
      on: {
        retry: () => {
          return {
            type: 'pending',
            effects: [{ type: 'loadData' }],
          };
        },
      },
    },
  },
  effects: {
    loadData: ({ dispatch }) => {
      /** Fetch some data and fire off more actions */
      fetch('...')
        .then(res => res.json())
        .then(data => {
          dispatch({ type: 'reportDataLoaded', data: JSON.stringify(data) });
        })
        .catch(() => {
          dispatch({ type: 'reportError' });
        });
    },
    showErrorMessage: ({ effect }) => {
      console.error(effect.message);
    },
  },
});

There's another example in examples/form.ts