applyMiddleware
Last updated
Last updated
Middleware is the suggested way to extend Redux with custom functionality. Middleware lets you wrap the store's method for fun and profit. The key feature of middleware is that it is composable. Multiple middleware can be combined together, where each middleware requires no knowledge of what comes before or after it in the chain.
The most common use case for middleware is to support asynchronous actions without much boilerplate code or a dependency on a library like . It does so by letting you dispatch in addition to normal actions.
For example, lets the action creators invert control by dispatching functions. They would receive as an argument and may call it asynchronously. Such functions are called thunks. Another example of middleware is . It lets you dispatch a async action, and dispatches a normal action when the Promise resolves.
Middleware is not baked into and is not a fundamental part of the Redux architecture, but we consider it useful enough to be supported right in the core. This way, there is a single standard way to extend in the ecosystem, and different middleware may compete in expressiveness and utility.
...middleware
(arguments): Functions that conform to the Redux middleware API. Each middleware receives 's and functions as named arguments, and returns a function. That function will be given the next
middleware's dispatch method, and is expected to return a function of action
calling next(action)
with a potentially different argument, or at a different time, or maybe not calling it at all. The last middleware in the chain will receive the real store's method as the next
parameter, thus ending the chain. So, the middleware signature is ({ getState, dispatch }) => next => action
.
(Function) A store enhancer that applies the given middleware. The store enhancer signature is createStore => createStore
but the easiest way to apply it is to pass it to as the last enhancer
argument.
If you want to conditionally apply a middleware, make sure to only import it when it's needed:
This makes it easier for bundling tools to cut out unneeded modules and reduces the size of your builds.
Middleware sounds much more complicated than it really is. The only way to really understand middleware is to see how the existing middleware works, and try to write your own. The function nesting can be intimidating, but most of the middleware you'll find are, in fact, 10-liners, and the nesting and composability is what makes the middleware system powerful.
Middleware only wraps the store's function. Technically, anything a middleware can do, you can do manually by wrapping every dispatch
call, but it's easier to manage this in a single place and define action transformations on the scale of the whole project.
If you use other store enhancers in addition to applyMiddleware
, make sure to put applyMiddleware
before them in the composition chain because the middleware is potentially asynchronous. For example, it should go before because otherwise the DevTools won't see the raw actions emitted by the Promise middleware and such.
Ever wondered what applyMiddleware
itself is? It ought to be an extension mechanism more powerful than the middleware itself. Indeed, applyMiddleware
is an example of the most powerful Redux extension mechanism called . It is highly unlikely you'll ever want to write a store enhancer yourself. Another example of a store enhancer is . Middleware is less powerful than a store enhancer, but it is easier to write.
To apply multiple store enhancers, you may use .