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

react-state-action-hooks

v1.1.0

Published

[![CircleCI](https://circleci.com/gh/hitochan777/react-state-action-hooks.svg?style=svg)](https://circleci.com/gh/hitochan777/react-state-action-hooks) ![npm](https://img.shields.io/npm/v/react-state-action-hooks.svg) [![code style: prettier](https://img.

Downloads

8

Readme

react-state-action-hooks

CircleCI npm code style: prettier

React hooks for managing state with (async) actions.

Installation

npm install react-state-action-hooks # for npm users
yarn add react-state-action hooks # for yarn users

Basic Usage

Demo is available on CodeSandbox.

First import useActionState react hook and ActionDefs which is a type definition.

import { useActionState, ActionDefs } from 'react-state-action-hooks';

Define a state and action definition types (or interfaces). State can be anything from number or string to nested object. Actions is an object each of whose keys is a (async) function that takes arbitrary number of parameters.

interface State {
  count: number;
}

type Actions = {
  incrementBy: (delta: number) => void;
  asyncReset: (interval: number) => Promise<void>;
  decrementBy: (delta: number) => void;
};

Then you can define the actual initial state and action definitions that comform to the types (interfaces) defined above. Action definition (actionDefs in the code below) is an object that has exactly the same keys action Actions, but the corresponding value is a function that returns function. The parameters of the outer function is the same as the ones defined in Actions. The parameters of the innner function is (state: State, actions: Actions) and it should return (acynchronously) a new state or nothing. You can also call as many other acitons as you want inside the action.

const initialState: State = {
  count: 0,
};

const actionDefs: ActionDefs<State, Actions, {}> = {
  incrementBy: (delta: number) => (state: State) => ({
    ...state,
    count: state.count + delta,
  }),
  asyncReset: (interval: number) => (state: State) => {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          ...state,
          count: 0,
        });
      }, interval);
    });
  },
  decrementBy: (delta: number) => (state: State, actions: Actions) => {
    actions.incrementBy(-1);
  },
};

Then finally in a React stateless component, you can use state and actions by invoking useActionState.

const Counter = () => {
  const { state, actions } = useActionState<State, Actions>(
    initialState,
    actionDefs
  );
  return (
    <div>
      <span>{state.count}</span>
      <button
        onClick={() => {
          actions.asyncReset(1000);
        }}
      >
        Reset after 1 sec
      </button>
      <button
        onClick={() => {
          actions.incrementBy(1);
        }}
      >
        +1
      </button>
      <button
        onClick={() => {
          actions.incrementBy(2);
        }}
      >
        +2
      </button>
      <button
        onClick={() => {
          actions.decrementBy(1);
        }}
      >
        -1
      </button>
    </div>
  );
};

Advanced Usage

Using context

useActionState can take the third parameter context which should be a key-value object, to which you can pass whatever values you want to have access to in an action. The context then becomes available as the third parameter of the returned function in an action definition. Here is an example:

interface Context {
  apolloClient: ApolloClient<any>;
}

const context = {
  apolloClient,
};

const Counter = () => {
  const { state, actions } = useActionState<State, Actions, Context>(
    initialState,
    actionDefs,
    context
  );
  /* lines below omitted */
};

Then the context becomes available in all the functions in action definition.

const actionDefs: ActionDefs<State, Actions, Context> = {
  someAction: () => async (
    state: State,
    actions: Actions,
    context: Context
  ) => {
    const { apolloClient } = context;
    /* do something with apolloClient */
  },
};

Contribution

Any kinds of contributions are welcome! Just submit issues or pull requests!

Author

Hitoshi Otsuki

License

MIT