簡體   English   中英

反應終極版中間件的#store.dispatch(動作)VS #next(動作)

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

我正在從文檔中學習有關React Redux中間件的知識,並想知道他所說的“操作實際上將再次遍及整個中間件鏈嗎?”的含義。 這是否意味着store.dispatch(action)將具有在中間件末尾返回的最終dispatch ,而next(action)僅指向中間件功能的特定點處的dispatch

確保從中間件而不是next(action)調用store.dispatch(action)確實有點技巧,該操作實際上將再次遍歷整個中間件鏈,包括當前的中間件。 如前所述,這對於異步中間件很有用。

在redux中, middleware是修補dispatch功能以擴展功能的功能。 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
  }
}

因此,當您return next(action)您會將dispatch函數的結果(應用了鏈中的所有先前middlewares return next(action)返回到下一個中​​間件。

但是,當您調用store.dispatch函數時,您將調用應用了所有 middlewares中間件鏈返回的最終dispatch方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM