Search code examples
reduxreact-router-redux

What exactly is Middlware in redux?


Okay so ive been having a hard time understanding middleware and the applyMiddleware in redux and routerMiddleware in react-router-redux. Can some one explain to me what exactly it is in simple terms. Thank you.


Solution

  • It's just a simple layer, which can transform / validate / logg data after dispatch(someAction(data)) but before handling this action.

    Common usages for middlewares is:

    • redux-thunk - middleware for handling async actions
    • redux-logger - Logging middleware for each action dispatch
    • You also can validate / prevent some actions if they are wrong or not acceptable in current state
    • You can transform some data before handling it in action
    • You can make some additional requests there, to mix it with dispatched data, but it's a bad practice

    You can imagine some other use cases, if you need to do something after each dispatch. For example you can store copies of your current store/state, to be able to revert data in future.