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

redux-quick-action

v1.0.5

Published

Redux action/reducer wrapper that hides synchronous reducers implementation

Downloads

4

Readme

redux-quick-action

Redux action wrapper that hides reducer implementation details

alt tag

Redux was designed in a very modular way, as a result developers are given full access to reducers and action types. It could be fairly tedious to setup an action flow. redux-quick-action helps abstract type strings, switch statements in reducers and many other implementation details away from you.

★ Action type strings ("LOG_IN", "SET_USER" etc.) are auto-generated and guaranteed unique (using shortid)

★ Write actions like the way you write reducers (see example below) and use them straight away

★ Build your aync actions on top of quick actions

ATTENTION: this module is essentially forcing a many(actions) to 1 (reducer) model, which is a subset of what redux offers (many to many). The many to many model was by design to allow actions to cause "side effects" when different reducers handles same action event, which is in some cases what people want to avoid or use combo actions (see below) instead.

Nontheless, quickActions should be treated as the smallest building blocks for more complex actions and each only responsible for change on one state tree.

a combo action that has side effects

import { action1, action2 } from 'quickActions1';
import { actionB } from 'quickActions2';

function doTwoThings(){
   action1();
   
   promise.then(()=>{
      action2();
   }).fail(actionB);
  
}

export { doTwoThings }

Install as an npm module

npm i redux-quick-action -S

API

import QuickActions from 'redux-quick-action';
const quickActions = new QuickActions(initialState, actionMap, options = {});

initialState: any. the initial state tree for the underlying reducer

actionMap: map. see example below

options

strict: false. whether to throw exception when reducer modifies the current state. This is usually supposed to be covered by your unit tests, however why not have them built-in?

namespace: ''. give a prefix for your action type. 'foo' -> e.g. foo/login/21ssdaf5

quickActions.toActions();   
//returns redux simple actions i.e. ()=> {type: 'LOG_IN', payload}

quickActions.toReducer(); 
//returns redux reducer handles all the action types configured. 
//Default case returns current state, just like the default case in your reducer switch block.

example/myQuickActions.js

import QuickActions from 'redux-quick-action';

const initialState = {
   id: 'foo'
};

const actionMap = {
    /**
     * write your quick actions similar to the way you write reducers
     * i.e. a function takes state and ...(rest of params), returns a new state
     */
    setUser(state, username, userid) {
        return Object.assign({}, state, {
            username,
            userid
        })
    },

    /**
     * Your redux action might not take any param
     */
    startLoading(state) {
        return Object.assign({}, state, {
            isLoading: true
        })
    },

    /**
     * you can modify your state (not recommended) and reducer will throw exception when in strict mode,
     */
    finishLoading(state) {
        return Object.assign(state, {
            isLoading: false
        })
    }

};

//quick actions in strict mode
export default new QuickActions(initialState, actionMap, {strict: true});

example/myReducers.js

import myQuickActions from './myQuickActions';

//yes, that's it. no more reducers and actions types.
export default myQuickActions.toReducer();

example/myActions.js

import myQuickActions from './myQuickActions';
import fetch from 'whatwg-fetch';

const { setUser, startLoading, finishLoading } = myQuickActions.toActions();

//writing more complex async actions using sync actions generated from your quick-actions.
function loadUser(userId) {
    startLoading(); //i.e. dispatch start loading redux event

    fetch(`/api/users/${userId}`)
        .then(response => {
            return response.json()
        })
        .then(user => {
            setUser(user.username, userId); //notice here you are using the generated actions, no need for state.
        })
        .finally(()=>{
            finishLoading(); //this will throw TypeError since we are in safe mode.
                            //change the quick action so that it does not modify the current state
        });
}

export {
    setUser,
    startLoading,
    finishLoading,
    loadUser
}

example/myStore.js

import { createStore, combineReducers, applyMiddleware } from 'redux';
import myReducers from './myReducers';


let myApp = combineReducers({
    user: myReducers,
    //article: articleReducers
    //...
});

export default createStore(myApp);