Understanding Redux middleware
Probably the easiest and best way of extending the Redux functionality is by using middleware.
There is a store enhancer function that comes in the Redux library named applyMiddleware
and allows you define one or multiple middleware functions. The way middleware works in Redux is simple, it allows you to wrap the dispatch
method of the store to extend its functionality. The same way as store enhancer functions, middleware is composable and has the following signature:
middleware = API => next => action => next(action)
Here, API
is an object containing the dispatch
and getState
methods from the store, destructuring the API
, the signature looks like this:
middleware = ({ getState, dispatch, }) => next => action => next(action)
Let's analyze how it works:
- The
applyMiddleware
function receives one or more middleware functions as arguments. For example:
applyMiddleware(middleware1, middleware2)
- Each middleware function is kept internally...