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-usereducermap

v0.1.3

Published

React reducer that maps action types and rules to handler functions

Readme

react-usereducermap

A React.useReducer replacement that maps one or more handler functions to an action.

Example

import React from 'react';
import { useReducerMap } from 'react-usereducermap';

export default function HelloWorld() {
  const [state, dispatch] = useReducerMap(
    {
      a: (state, data, meta) => {
        return { hey: 'I got called' };
      },
    },
    {}
  );

  return (
    <div className="App">
      <h1
        onClick={(evt) => {
          dispatch({ type: 'a', data: 'this is some data' });
        }}
      >
        Hello react-usereducermap
      </h1>
      <pre>{JSON.stringify(state, null, 2)}</pre>
    </div>
  );
}

Install

npm i react-usereducermap

Import

import { useReducerMap } from 'react-usereducermap';

Construction

const [state, dispatch] = useReducerMap(actionMap, initialState);

Dispatching

The dispatch function is the same one returned from useReducer.

The dispatched object must have a type property that will be mapped to the action handler. Objects missing a type will throw an error indicating that no handler was mapped to the action. Warning: I may add validation on the type later to provide a more specific error message.

ActionMap

An object where the keys are actions that are dispatched through the reducer and values that are the handlers.

function actionHandler(state, data, meta) {
    // does something
    // returns the modifications to the state
    return { newStateVal: "Hey hey!!" };
}

function anotherActionHandler(state, data, meta) {
    // does something
    // returns the modifications to the state
    return { newStateVal: "Hey hey!!" };
}

const actionMap = {
    // Single action handler
    OnClick: actionHandler,
    // multiple action handlers, executed sequentially
    OtherClick: [actionHandler, anotherActionHandler],
    // multiple action handlers with helper data and functions.
    // Helpers are added to the handler's meta object as they are named. Don't overwrite dispatch or type.
    yetAnotherClick: [actionHandler, [anotherActionHandler, { someData: 'extra data', otherFunction: ()=>{}}]],
    // Called on all events before action handlers
    pre: (state, data, meta) => {},
    // Called on all events after action handlers have executed
    post: (state, data, meta) => {},
}

const [state, dispatch] = useReducerMap(actionMap, {});
dispatch({type: 'OnClick', ignoredData: "I'm not being used"})

console.log(JSON.stringify(state, null, 2))
/* Results:
{
    "newStateVal": "Hey hey!!"
}

ActionHandler

function actionHandler(state, data, meta) {
  // does something
  // returns the modifications to the state
}
  • state: The reducer's current state object.
  • data: The remaining attributes of the dispatch.
  • meta: An object containing the original action type, dispatch function for the reducer, and extra helpers.
// data example
// If dispatch were called like this:
dispatch({ type: 'OnClick', ignoredData: "I'm not being used" });
// Then data will contain
{
  ignoredData: "I'm not being used";
}
// meta example
// If dispatch were called like this:

dispatch({type: 'OnClick', ignoredData: "I'm not being used"})
// Then meta will contain this:
{
    type: 'OnClick',
    dispatch: dispatch // original dispatch function
}

The function returns a state that shallow merges into the original state.

You can dispatch from an action handler. You can cause infinite loops, so watch out!

Reserved Action Handlers

  • pre: Executed before all other action handlers.
    • Only one pre handler and must be a function
    • Can modify state
    • meta object does not contain the dispatch function.
    • Dispatching an object like {type: 'pre'} will throw an exception.
  • post: Executed after all other action handlers.
    • Only one post handler and must be a function
    • Can modify state
    • meta object does not contain the dispatch function.
    • Dispatching an object like {type: 'post'} will throw an exception.
  • patterns: Not implemented yet
    • Maps a regex to a set of action handlers
    • Evaluated after explicit action handlers
    • Good for handling events with a common prefix. Like if all your API callers will prefix an error action like {type: 'ERROR_API_GETHOST'} and {type: 'ERROR_API_GETUGLYPHOTO'}, then you can have a regex like /^ERROR/ and handle the API errors in a common way rather than in an explicitly named action handler for each.
    • Throws a TypeError if you try to use it.
    • Dispatching an object like {type: 'patterns'} will throw an exception.