简体   繁体   English

反应终极版中间件的#store.dispatch(动作)VS #next(动作)

[英]React Redux middleware's #store.dispatch(action) vs #next(action)

I'm learning about react redux middleware from the docs and want to know what he means by "the action will actually travel the whole middleware chain again?" 我正在从文档中学习有关React Redux中间件的知识,并想知道他所说的“操作实际上将再次遍及整个中间件链吗?”的含义。 Does that mean the store.dispatch(action) will have the final dispatch that is returned at the end of the middleware, whereas next(action) will only point to the dispatch at a certain point of the middleware function? 这是否意味着store.dispatch(action)将具有在中间件末尾返回的最终dispatch ,而next(action)仅指向中间件功能的特定点处的dispatch

It does a bit of trickery to make sure that if you call store.dispatch(action) from your middleware instead of next(action), the action will actually travel the whole middleware chain again, including the current middleware. 确保从中间件而不是next(action)调用store.dispatch(action)确实有点技巧,该操作实际上将再次遍历整个中间件链,包括当前的中间件。 This is useful for asynchronous middleware, as we have seen previously. 如前所述,这对于异步中间件很有用。

In redux middleware is a function that patches dispatch function to extend functionality. 在redux中, middleware是修补dispatch功能以扩展功能的功能。 And next function inside middleware is in fact just a copy of store.dispatch function. middleware next功能实际上只是store.dispatch函数的副本。

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

So when you return next(action) you return a result of dispatch function, with all previous middlewares in the chain applied, to the next middleware. 因此,当您return next(action)您会将dispatch函数的结果(应用了链中的所有先前middlewares return next(action)返回到下一个中​​间件。

But when you call store.dispatch function you call a final dispatch method return from middleware chain with all middlewares applied. 但是,当您调用store.dispatch函数时,您将调用应用了所有 middlewares中间件链返回的最终dispatch方法。

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

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