1.2.0 • Published 6 years ago

dispatch-hijack v1.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Dispatch Hijack

Dispatch Hijack middleware allows you to write redux side effects on a per-action basis.

Build Status npm version semantic-release

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;
  }
};