
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
redux-handler-middleware
Advanced tools
Middleware that executes custom handlers before and after state updates.
Middleware that executes custom handlers before and after state updates. This can be especially useful for handling navigation redirects.
npm install redux-handler-middleware --save
middleware/handlerMiddleware.js
import browserHistory from 'react-router/lib/browserHistory';
import createHandlerMiddleware from 'redux-handler-middleware';
import {FETCH_POST_FAILURE, FETCH_POSTS_FAILURE} from 'constants/blog';
const handlerMiddleware = createHandlerMiddleware([{
actions: [FETCH_POSTS_FAILURE, FETCH_POST_FAILURE],
afterHandler: (store, action) => {
if(action.payload.status === 404){
browserHistory.push('/404');
}
}
}]);
export default handlerMiddleware;
store/createStore.js
import { createStore, applyMiddleware, combineReducers } from 'redux';
import handlerMiddleware from 'middleware/handlerMiddleware';
import rootReducer from 'reducers';
export default createStore(
rootReducer,
{},
applyMiddleware(handlerMiddleware)
);
So now when a FETCH_POSTS_FAILURE
or a FETCH_POST_FAILURE
action is dispatched, the afterHandler will be invoked after the state updates and it will redirect you to /404
. This provides a simple and intuitive solution to navigating in response to actions.
createHandlerMiddleware
- Function
Must be passed an array of plain objects.
action
- String
The type of the action. This is used when determining which handlers to fire.
actions
- Array of actions
This is useful when multiple action types use the same handler.
beforeHandler
- Function
Handler to be called before the state is updated.
afterHandler
- Function
Handler to be called after the state is updated.
to Diego Castillo who came up with the original idea in his blog post.
FAQs
Middleware that executes custom handlers before and after state updates.
We found that redux-handler-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.