• Stars
    star
    101
  • Rank 338,166 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Dispatch your action one by one with previously updated store

Redux Sequence Action

A middleware enabling sequential action dispatch for Redux.

Build Status npm version

$ npm install --save redux-sequence-action

Why

Suppose you have a AddressPicker component which let user select the delivery address. It consists of 2 select with state list and city list. User can picks a state then a city.

select

So you will have the following action creators:

  • selectState (stateId)
  • selectCity (cityId)

However, when user changes a state, the city list should be updated accordding to new state. For action creator selectState, it actually does the duty of selectState and selectCity.

For example, suppose we must dispatch some actions in certain order: A => B & C => D => E. A is a sync action and others are async actions. So we do this:

dispatch((dispatch, getState) => {
    dispatch(A);
    Promise.all(dispatch(B), dispatch(C)).then(() => {
        return dispatch(D);
    }).then(() => {
        dispatch(E);
    });
})
// A => B & C => D => E
// A ~ E are actions

It need apply a thunk middleware which dispatch function like action, and a fetch middleware which is responsible for getting API data and return a promise.

store => next => action => {
  //return a promise here
  return asyncAction(url, params).then(
    data => {
      return next({...action, payload: data, type: successType})
    }, e => {}
  )
}

If you use redux-sequnce-action, you can merely write declarative code like this:

dispatch([
    A,
    [B, C],
    D,
    E
])

Yes, we only provide a syntax sugar.

How

To better reuse our code, we can dispatch a action that dispatchs more action in sequence, looks like this:

function selectState(stateId) {
  return [
    {
      type: 'SELECT_STATE',
      payload: stateId
    },
    (dispatch, getState) => {
      // `getState()` returns the state (or store) which is computed through
      // first action, so you can use this updated store to find out needed
      // portion and pass it to next action creator
      const {cityId} = getState().cityList[0];
      dispatch(selectCity(cityId))
    }
  ]
}

function selectCity(cityId) {
  return {
    type: 'SELECT_CITY',
    payload: cityId
  };
}

When we call selectState(13), this action creator will first dispatch a SELECT_STATE action with payload 13. Our reducer should update and return the new state (or store).

Then it will dispatch another action defined as the second element in steps array. Inside this function, we can get updated store and find out wanted part of the store and pass it to next action.

Usage

$ npm install --save redux-sequence-action

Then, to enable Redux Sequence Action, use applyMiddleware():

import { createStore, applyMiddleware } from 'redux';
import sequenceAction from 'redux-sequence-action';
import rootReducer from './reducers/index';

// create a store that has redux-sequence-action middleware enabled
const createStoreWithMiddleware = applyMiddleware(
  sequenceAction
)(createStore);

const store = createStoreWithMiddleware(rootReducer);

As your action creator, it should return an array of actions.

Scripts

$ npm run test

License

MIT

More Repositories

1

a-cartoon-intro-to-redux-cn

看漫画,学 Redux。不写一行代码,轻松看懂 Redux 原理!
HTML
768
star
2

react-anything-sortable

A ReactJS component that can sort any children with touch support and IE8 compatibility
JavaScript
459
star
3

react-menu-aim

A React mixin version of Amazon's jQuery-menu-aim plugin
JavaScript
160
star
4

react-marquee

A <marquee> component for React
JavaScript
81
star
5

redux-form-utils

Ease the pain of handling form bindings in Redux
JavaScript
73
star
6

china-city-select

简单可用的中国省份、城市、县区三级联动选择组件
JavaScript
60
star
7

react-lifecycle

A ReactJS mixin for logging component lifecycle method call
JavaScript
54
star
8

redux-composable-fetch

A fetch middleware with extensible and opt-in functionalities like cache or log for Redux
JavaScript
37
star
9

react-redux-async-example

A react, redux, ajax working example
JavaScript
33
star
10

bjut_crawler

BJUT secrets~
JavaScript
32
star
11

alarm

A simple & super fast & responsive Ghost theme
CSS
31
star
12

undefinedblog

我的个人博客(源码)
CSS
23
star
13

test-react

JavaScript
8
star
14

hexo-migrator-ghost

Ghost migrator for Hexo.
JavaScript
7
star
15

healthreport

A toy app to analyze health report and ask questions
JavaScript
5
star
16

In-page-Highlighter

Instantly highlight anything you select in current page (Chrome plug-in)
JavaScript
4
star
17

gulp-legacy-ie-css-lint

Ensure your CSS won't be discarded by IE 9 and below because of crazy stylesheets limitation of legacy IE.
JavaScript
3
star
18

grunt-wordpress

Simple & working Grunt configuration set for Wordpress development
JavaScript
3
star
19

easy-js-perf

A human readable wrapper of `window.performance` API
JavaScript
2
star
20

ultimate-gitlab-gitbook

Receive Gitlab web hook, generate content using Gitbook, enhance search functionality using ElasticSearch.
JavaScript
2
star
21

Ali_learning

JavaScript
2
star
22

webpack-debug-plugin

Inject useful debug info to each entry in the runtime
JavaScript
2
star
23

jsbin-nude

JS Bin with NO Backend Dependency
JavaScript
1
star
24

react-analyzer

Give you a human readable structure of your React component
JavaScript
1
star
25

canon-blog

Blog for canon
PHP
1
star
26

ceresdb

A simple node.js client for CeresDB
TypeScript
1
star
27

excel-gantt-chart

Automatically exported from code.google.com/p/excel-gantt-chart
1
star
28

jasonslyvia.github.io

1
star
29

grunt-wp-replace

Replace new static assets reference(js or css) in Wordpress theme source file
JavaScript
1
star
30

canon

Just another WordPress theme.
JavaScript
1
star