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

dumb-reducer

v2.0.3

Published

A generic redux reducer for simple cases

Downloads

47

Readme

GitHub license NPM version

What is this?

It's a reducer factory function that creates redux action handlers for any action beginning with a set prefix, these handlers will put the action's payload directly into the state. An optional initialState can be set if needed, as well as a subReducers parameter that allows you to set special rules for special cases and behaves a bit like combineReducers.

How do I use it?

Let's say you have this component:

const myForm = ({ submit, isFetching, error, success }) => (
  <form onSubmit={submit}>
    ...
    <Button type="submit">
      Go!
      {isFetching && <Spinner />}
    </Button>
    {error && 'An error occurred!'}
    {success && 'A success occurred!'}
  </form>
);

The form's onSubmit callback would eventually dispatch these actions to display or hide the Spinner element, the error, or the success state:

const submitStart = () => ({
  type: 'formSubmit/start',
  isFetching: true,
  error: null,
  success: false,
});

const submitSuccess = () => ({
  type: 'formSubmit/success',
  isFetching: false,
  success: true,
});

const submitError = (error) => ({
  type: 'formSubmit/error',
  isFetching: false,
  error,
});

You would also need reducers to handle these actions:

export default (state = { isFetching: false, error: null, success: false }, action = {}) => {
  switch (action.type) {
    case 'formSubmit/start':
      return {
        ...state,
        isFetching: action.isFetching,
        error: action.error,
        success: action.success,
      };
    case 'formSubmit/success':
      return {
        ...state,
        isFetching: action.isFetching,
        success: action.success,
      };
    case 'formSubmit/error':
      return {
        ...state,
        isFetching: action.isFetching,
        error: action.error,
      };
    default:
      return state;
  }
};

You'll notice that, for the most part, we could just put whatever is in the action directly into the state without looking. This lib is here to replace that example with this:

import { makeDumbReducer } from 'dumb-reducer';

export default makeDumbReducer(
  'formSubmit', // prefix
  { isFetching: false, success: false, error: null }, // initial state
);

That's it! It forwards the payload.

Sub-reducers

You may need to have different/more complex behaviours in parts of the state, makeDumbReducer allows you to set sub-reducers through the third parameter. Here is a an example with a sub-reducer that keeps an auto-incrementing counter of the number of times it was called:

import { makeDumbReducer } from 'dumb-reducer';

export default makeDumbReducer(
  'myState', // prefix
  { counter: 0 }, // initial state
  { counter: (state, action) => state + 1 }, // sub-reducers
);

// the sub-reducer will be called if you dispatch any of these actions:
{ type: 'myState/counter' };
{ type: 'myState/counter/doStuff' };

Note that sub-reducers only have access to (and return) the sub-state they are assigned to, which is very similar to combineReducers.

Deleting a key

If you set a key to undefined in the action, the key will be deleted in the state.

// initial state is `{}`
dispatch({ type: 'myState/set', isSet: true }); // state is now `{ isSet: true }`
dispatch({ type: 'myState/unset', isSet: undefined }); // state is now `{}`

Helper to prefix actions

There's also a helper that automatically prefixes actions by adding the type and should help make action code smaller too:

// Reducer
import { makeDumbReducer } from 'dumb-reducer';

export default makeDumbReducer(
  'formSubmit', // prefix
  { isFetching: false, success: false, error: null }, // initial state
);


// Actions
import { prefixActions } from 'dumb-reducer';
const actions = prefixActions(
  'formSubmit', // prefix
  {
    start: () => ({ isFetching: true, error: null, success: false }),
    success: () => ({ isFetching: false, success: true }),
    error: error => ({ isFetching: true, error }),
  }, // actions
);

// later...
dispatch(actions.start());
// will dispatch { type: 'formSubmit/start', isFetching: true, error: null, success: false }
dispatch(actions.error(myError));
// will dispatch { type: 'formSubmit/error', isFetching: false, error: myError }

This helper also accepts nested objects, useful with sub-reducers. Here is an example for a state containing a hasSession flag and a list of users like { <user id>: <user> }:

// reducer
import { makeDumbReducer } from 'dumb-reducer';

export default makeDumbReducer(
  'myState', // prefix
  { hasSession: false, users: {} }, // initial state
  { users: makeDumbReducer('users') }, // sub-reducers, with a nested dumb-reducer for the users
);


// actions
import { prefixActions } from 'dumb-reducer';
const actions = prefixActions(
  'myState', // prefix
  {
    setHasSession: hasSession => ({ hasSession }),
    setAllUsers: users => ({ users }),
    users: {
      add: user => ({ [user.id]: user }),
      remove: userId => ({ [userId]: undefined }),
    },
  }, // actions
);

// later...
dispatch(actions.setHasSession(true));
// will dispatch { type: 'myState/setHasSession', hasSession: true }
dispatch(actions.users.add(selfUser));
// will dispatch { type: 'myState/users/add', [selfUser.id]: selfUser }

Quirks and limitations

  • <prefix>/stuff is a valid action type, <prefix>_stuff and <prefix> are not. The / is expected. This is also true when using sub-reducers, <prefix>/<subprefix>/stuff is valid while <prefix>/<subprefix> is not.
  • You can put dumb-reducers in your dumb-reducer, although this expects a static state structure. It doesn't work if you have a dynamically changing list objects (like { <user id>: <user> }). You'll need something a bit smarter if you want to, say, update the email of a user in your local redux-user-cache without putting the whole user in the action.
  • You can't have a key called type in the state without using sub-reducers.
  • It really isn't ideal when you have to work with arrays. It's usually better to use a dict with { <object.id>: <object> }, but if you really need to add/remove elements in an array in a dumb-reducer, you can use the subReducers param for the specific key that is an array.