I always thought redux-batched-actions
was great to clean up your action log, but using it is a bit verbose. Why should I have to import and call batchActions
all the time and hardcode the action names? Just passing an array to dispatch
and joining the action or function names with commas seemed like a better API to me. And that's exactly what this micro library does!
Since arrays as actions seem very uncommon in redux middleware, this is unlikely to clash with most other middlewares.
npm install redux-auto-batched-actions
import { createStore, applyMiddleware } from 'redux';
import { enableBatching } from 'redux-batched-actions';
import { autoBatchingMiddleware } from 'redux-auto-batched-actions';
const setA = payload => ({type: 'SET_A', payload});
const setB = payload => ({type: 'SET_B', payload});
const reducer = (state={a:0, b:0}, action) => {
switch (action.type) {
case 'SET_A': return {...state, a: action.payload}
case 'SET_B': return {...state, b: action.payload}
default: return state
}
}
// Handle bundled actions in reducer
const store = createStore(
enableBatching(reducer),
applyMiddleware(autoBatchingMiddleware)
);
store.dispatch([setA(1), setA(2), setB(5)]);
store.getState(); // {a: 2, b: 5}