In some redux tutorials I came across the following way to declare a function that will act as middleware, the question is what is happening in this part of the function: (store) => (next) => (action) =>
const logger = (store) => (next) => (action) => {
// logger body...
next(action)
}
I mean take input store and then what happens?
The function you show is the ES6 way of writing the following function:
This is the basic structure of a middleware for redux, which you will then pass as a parameter in
applyMiddleware(logger)
, where internally it expects to receive that structure of 3 nested functions.Why there are three functions, the vast majority of the time you should not worry and simply write your code inside the last nested function, but if you are still in doubt, what it does in each case would be something like this:
Here are some examples where you need to define code for more than one of these functions.
arguments
Middlewares
( Arguments ): Functions that comply with thatRedux API
of middleware . Each middleware receivesStore's
dispatch
andgetState
functions as named arguments, and returns a function. That function will be given thenext
middleware dispatch method, and is expected to return an action function callednext(action)
with a potentially different argument, or at a different time, or perhaps not call it at all. The last middleware in the chain will receive from the actual storedispatch
method thatnext
parameter, thus ending the chain. Thus, the signature is the middleware({ getState, dispatch }) => next => action
.Returns _
(Function) enhancer A store that applies the given middleware. The signature store enhancer is
createStore => createStore
but the easiest way to implement it is to pass it tocreateStore()
as the lastenhancer
argument.Example see here