简体   繁体   English

redux文档中的这个示例如何工作?

[英]How does this example from the redux documentation work?

I am reading the Redux documentation and it lists different ways of applying middleware that could work, but are not efficient before it gets to how Redux actually applies middleware. 我正在阅读Redux文档,它列出了应用中间件的不同方法,这些方法可以工作,但是在了解Redux如何实际应用中间件之前效率不高。

I have no idea why this following method would work even though I see why it is not efficient: 我不知道为什么以下这种方法为什么行得通,即使我知道为什么它效率不高:

 function applyMiddlewareByMonkeypatching(store, middlewares) { middlewares = middlewares.slice() middlewares.reverse() // Transform dispatch function with each middleware. middlewares.forEach(middleware => store.dispatch = middleware(store) ) } // We could use it to apply multiple middleware like this: applyMiddlewareByMonkeypatching(store, [logger, crashReporter]) 

How could that possibly work? 那怎么可能工作呢? Wouldn't you just be overwriting the store.dispatch() function with every iteration of the forEach loop, and in the end all you would be applying is the last middleware in the array? 您难道不只是要在forEach循环的每次迭代中覆盖store.dispatch()函数,最后您要使用的只是数组中的最后一个中间件?

Here is a link to the documentation: http://redux.js.org/docs/advanced/Middleware.html 这是文档的链接: http : //redux.js.org/docs/advanced/Middleware.html

After going through the docs, I believe I have an answer for you. 阅读完文档后,我相信我会为您解答。

Let's take the logger function for example. 让我们以记录器功能为例。 Here it is below. 在这里。

function logger(store) {
  let next = store.dispatch

  // Previously:
  // store.dispatch = function dispatchAndLog(action) {

  return function dispatchAndLog(action) {
    console.log('dispatching', action)
    let result = next(action)
    console.log('next state', store.getState())
    return result
  }
}

Here is the call to the logger function below 这是下面对记录器功能的调用

store.dispatch = logger(store)

So the store instance gets passed to logger . 因此, store实例将传递给logger Once inside the logger function, the next variable holds the reference to the original store.dispatch function. 进入logger函数后, next变量将保留对原始store.dispatch函数的引用。 Notice it has not been overwritten yet. 请注意,它尚未被覆盖。 Now, you dispatch the action using this line next(action) and store that in result and return the result value. 现在,您使用此行next(action)调度动作并将其存储在result并返回result值。

The whole reason you are overwriting the store.dispatch function is so that you don't have to make an explicit call to any middleware you are trying to apply. 覆盖store.dispatch函数的全部原因是,您不必显式调用要尝试应用的任何中间件。

From the docs 来自文档

Why do we even overwrite dispatch? 为什么我们甚至覆盖调度? Of course, to be able to call it later, but there's also another reason: so that every middleware can access (and call) the previously wrapped store.dispatch 当然,以后可以调用它了,但是还有另一个原因:每个中间件都可以访问(并调用)以前包装的存储。

It is by the magic of closures. 这是关闭的魔力。

Original Dispatch function in stored in next variable 原始调度功能存储在下一个变量中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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