dispatch-hijack
v1.2.0
Published
Hijack dispatched actions before they reach reducers -- without writing middleware
Downloads
13
Maintainers
Readme
Dispatch Hijack
Dispatch Hijack middleware allows you to write redux side effects on a per-action basis.
Motivation
Redux action side effects can be managed based on action.type
with a switch statement within a custom middleware. However, this case
logic can be a pain to manage and tend to be too abstracted from the original action that triggers it.
Dispatch Hijack allows side effects to be written on a per-action basis -- without complicated case
logic.
Installation
npm install --save dispatch-hijack
Then, to enable Dispatch Hijack, use applyMiddleware()
:
import { createStore, applyMiddleware } from "redux";
import hijack from "dispatch-hijack";
import rootReducer from "./reducers/index";
// Note: this API requires redux@>=3.1.0
const store = createStore(rootReducer, applyMiddleware(hijack));
Basic Usage
Add the hijack
key to any action. The hijack
key will be removed before passing the action to the next middleware or reducer.
const normalAction = {
type: "ACTION"
};
const hijackedAction = {
type: "ACTION",
hijack: action => {
console.log("Hijacked");
return action;
}
};