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 🙏

© 2025 – Pkg Stats / Ryan Hefner

redux-callback-reducer

v0.1.1

Published

Redux wrapper/decorator to create reducers

Downloads

8

Readme

redux-callback-reducer

redux-callback-reducer allows any function or class method to make a reducer for your state, without adding any additional actions.

Installation

npm install --save redux-callback-reducer

Tutorial

To use redux-callback-reducer you need to configure your state using generateReducers or createCallbackReducer, prepare a decorator or wrapper function, and make your functions as state reducer.

To use the decorator you need to configure babel-plugin-transform-decorators-legacy

Configure state

You can use generateReducers to generate a nested state or generate your state using combineReducers from redux and createCallbackReducer from redux-callback-reducer

  • Using generateReducers:
import { combineReducers } from 'redux'
import { generateReducers } from 'redux-callback-reducer';

const rootReducer = combineReducers({
    users: generateReducers('users', {
        items: [],
        isLoading: false
    }),
});
  • Using combineReducers and createCallbackReducer:
import { combineReducers } from 'redux'
import { createCallbackReducer } from 'redux-callback-reducer';

const rootReducer = combineReducers({
    admins: combineReducers({
        items: createCallbackReducer('admins.items', []),
        isLoading: createCallbackReducer('admins.isLoading', false),
    })
});
  • You can create a flat state using createCallbackReducer:
import { combineReducers } from 'redux'
import { createCallbackReducer } from 'redux-callback-reducer';

const rootReducer = combineReducers({
    admins: createCallbackReducer('admins', {}),
});

Creating a wrapper or decorator

You need to create a wrapper for functions or decorator for methods.

  • Wrapper:
import { store } from './store';
import { createWrapper } from 'redux-callback-reducer';

export const wrapper = createWrapper(store);
  • Decorator:
import { store } from './store';
import { createDecorator } from 'redux-callback-reducer';

export const reducer = createDecorator(store);

Using wrapper or decorator:

  • Wrapper:
import { wrapper } from '../wrapper';

export const setAdmins = wrapper('admins.items', (items, state) => items);

export const getAdmins = () => fetch('/api/admins/').then(setAdmins);
  • Decorator:
import { decorator as reducer } from '../decorator';

export class UserStateService { 
    static getUsers() {
        return fetch('/api/users/')
            .then(this.setUsers);
    }
    
    @reducer('users.items')
    static setUsers(users, state) {
        return users;
    }
}
  • If you do not create nested state, you can write a reducer as follows:
import { decorator as reducer } from '../decorator';

export class UserStateService { 
    static getUsers() {
        return fetch('/api/users/')
            .then(this.setUsers);
    }
    
    @reducer('users')
    static setUsers(items, state) {
        return {
            ...state,
            items,
        };
    }
}

Example

  • example - basic reference implementation

API

createCallbackReducer(reducerName, initialState)

generateReducers(reducerName, initialState)

createDecorator(store)

createWrapper(store)