Middleware that lets you call predefined functions on certain actions.
npm i -S redux-trigger-middleware
git clone https://github.com/saintcrawler/redux-trigger-middleware.git && cd redux-trigger-middleware
npm i && npm test
It just sits in your middleware chain and listens for trigger actions. You must supply it with triggers configuration object.
There are 3 types of triggers:
- By action type. Triggers if
action.type === trigger.type
. - By predicate. Triggers if
pred(action)
returnstrue
. - By array of action types. Triggers if
[type1, type2, type3].some(t => t === action.type)
.
Here is an example config object:
const config = {
actions: [
{
trigger: 'STRING_TRIGGER',
handler: (action, dispatch) => {
dispatch({type: 'iHaveBeenTriggered!'});
}
},
{
trigger: (action) => { return action.payload === 777 },
handler: (a, d) => d({type: 'lucky!'});
},
{
trigger: ['SOMEBODY', 'CALL', 'ME!'],
handler: (a, d) => d({type: 'thanks'});
}
]
}
As you may guess handler
is a function that will be called on successful trigger.
In order to capture as most actions as possible, you may want to place this middleware at the end of the chain (but, probably, before some loggers, such as redux-logger
). That's because your other middlewares can dispatch actions too and, sure, you want to catch 'em all.
Here is the complete example:
import Trigger from 'redux-trigger-middleware'
const trigger = new Trigger(config); // `config` is from above
const enhancer = applyMiddleware(
//thunk,
//myOtherMiddleware,
trigger.middleware,
//logger
);
const store = createStore(reducer, {}, enhancer);
// And use it as you wish...
Also, this is a nice way to perform api calls. Use it together with redux-call-api.
ISC