简体   繁体   中英

Why we're doing slice() & reverse() in redux applyMiddleware function?

In the official doc of redux middleware .

It has a show case of redux applyMiddleware function:

function applyMiddlewareByMonkeypatching(store, middlewares) {
  middlewares = middlewares.slice()
  middlewares.reverse()

  // Transform dispatch function with each middleware.
  middlewares.forEach(middleware =>
    store.dispatch = middleware(store)
  )
}

I can not understand why it's doing slice() & reverse() here.

// This can make a deep copy of middlewares parameter.
middlewares = middlewares.slice()

But why we're making a deep copy, and why we have to reverse the array?

It makes me so confused. Please help!

I found the answer here:

Redux: The Middleware Chain

It says:

Finally, I'd like to fix the order in which middlewares are specified. They are currently specified in the order in which the dispatch function is overridden. However, it would be more natural to specify the order in which the action propagates through the middlewares.

It is more clarified if we saw the middlewares order in action go through order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM