redux-event-bus
Middleware for redux that allows you to subscribe to actions that are dispatched to a redux store.
Installation
Install the package
npm install --save redux-event-bus
Then, to enable redux-event-bus:
const EventBus createEventBusMiddleware } = ;const applyMiddleware createStore } = ;const rootReducer = ; const evBus = ; const evBusMiddleware = ; const store = ;
If you are using other middlewares
const EventBus createEventBusMiddleware } = ;const applyMiddleware compose createStore } = ;const rootReducer = ; const evBus = ; const middlewares = //Other middlewares ; const store = ;
If you are using ES6 Imports, replace the line below using require with the import statement below in either example above.
//replaceconst EventBus createEventBusMiddleware } = ;//with;
Usage
Listen for events for a particular action
const store = ;// From above//Importsconst evBus = ;// Hook into redux e.x.: applyMiddlewareevBus; store; //Will call function above store; //Will call function above
Listen for events for a particular action only once
const store = ;// From above//Importsconst evBus = ;// Hook into redux e.x.: applyMiddlewareevBus; store; //Will call function above store; //Will not call function above since it already has been called.
Cancel events
const store = ;// From above//Importsconst evBus = ;// Hook into redux e.x.: applyMiddlewareconst cancelListener = evBus;;store; //Will not call listener above since it was cancelled. const handler = {};evBus;evBus; store; //Will not call listener above since it was cancelled.
License
Copyright 2018 Jonah Nestrick
MIT License