2016-08-16 15 views
0

我正在學習docs中的reactx中間件,並且想知道他的意思是「該動作實際上會再次穿過整個中間件鏈? 「這是否意味着store.dispatch(action)將具有在中間件末尾返回的最終dispatch,而next(action)將只在中間件功能的某個點處指向dispatchReact Redux中間件的#store.dispatch(動作)對#下一個(動作)

它有點掛羊頭賣狗肉,以確保如果你從你的中間件,而不是下一個(動作)調用 store.dispatch(動作), 動作將再次實際行駛的整個中間件鏈, 包括目前的中間件。正如我們以前所見,這對於異步 中間件非常有用。

+1

可能重複[Redux中間件中dispatch和next之間有什麼區別?](http://stackoverflow.com/questions/36160457/whats-the-difference-between-dispatch-and-next-in-redux-中間件) –

+0

見http://jsfiddle.net/ca73kt2v/ – qwertymk

回答

2

在還原middleware是補丁功能dispatch函數來擴展功能。而next功能裏面的middleware實際上只是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中應用的鏈,下一個中間件。

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

+0

接下來是未打補丁的調度,它是調用以前調度的新調度[docs](https://github.com/gaearon/redux-thunk) – dmi3y

相關問題