0

redux-dispatcher

 2 years ago
source link: https://www.npmjs.com/package/redux-dispatcher
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

redux-dispatcher is an all-in-one simple solution to manage actions with less code 🦄

Its main purpose is to combine action type, action creator and dispatch function into one, then you no need to worry about defining and managing action type constants.

Short example

import { createDispatcher } from 'redux-dispatcher'

const key = "app"

const mapDispatch = {
    setUser: name => ({ name })
};

const appDispatcher = createDispatcher(key, mapDispatch)
appDispatcher.setUser("Anonymous")  // dispatch action {type: "app/SET_USER", name: "Anonymous"}

Guaranteed:

  • Intuitive
  • Less code

Suitable:

  • If you use Redux and want to reduce boilerplate
  • If you find it tiresome every time you need to define, import, manage actions

Advanced functionalities:

Usage

1. Install and setup

npm install redux-dispatcher --save

Setup

import {applyMiddleware, createStore} from "redux";
import {dispatcherMiddleware} from "redux-dispatcher";

const store = createStore(
    reducer,
    applyMiddleware(dispatcherMiddleware)
);

2. Define action type, action creator and dispatch action

With redux-dispatcher, the action type is implicitly computed according to the key passed to createDispatcher method and the name of the action creator.

But if you want to explicitly specify a type, you can include a type property in the return object.

import { createDispatcher } from 'redux-dispatcher';

const key = "profile";

const mapDispatchToAC = {
    // type = "profile/FETCH_PROFILE"
    // if the action doesn't depend on parameters, you can just write a plain object
    fetchProfile: {loading: true},

    // if not explicitly specified, the action type will be automatically set: type = "profile/UPDATE_PROFILE"
    updateProfile: (username, password) => ({
        // type: "UPDATE_PROFILE",     you can specify the action type here
        username, password
    })
};

const profileDispatcher = createDispatcher(key, mapDispatchToAC);

To dispatch action, you can just import the dispatcher you need and dispatch action anywhere you want

profileDispatcher.updateProfile("my_username", "my_password");

3. Handle action in reducer

Create reducer with redux-dispatcher is as easy as create a usual reducer, with less code.

import { createReducer } from 'redux-dispatcher';

const mapActionToReducer = () => ({
     // similar to fall-through case in switch statement
     [[
       profileDispatcher.fetchProfile,
       profileDispatcher.reloadProfile,
       profileDispatcher.resetProfile
     ]]: (state, payload) => payload,    // notice the payload, it doesn't have "type" property like action
     
     [profileDispatcher.loadingProfile]: {loading: true},    // you can just write a plain object if new state doesn't computed from current state or action payload
     
     [profileDispatcher.updateProfile]: (state, {username, password}) => ({
       username,
       password: encrypt(password)
     })    // only return what data need to be merged in state
     
     // the default case is handled automatically
})

const profileReducer = createReducer(initialState, mapActionToReducer);

const rootReducer = combineReducers({
  profile: profileReducer,
});

4. Advanced functionalities

This section describes some useful features and extensions you may find interesting like thunk and immutable helper.

Retrieve side effect result after dispatching an action

When your action trigger some side effect (like fetching API), you can use built-in hooks dispatchResult or waitResult to dispatch and subscribe for results from action.

(Available from v1.9.6)


Define thunk like your favourite Redux Thunk

See example

Immutable helpers for state

See example

Easily manage action types

See example


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK