![](/img/trans.png)
[英]'TypeError: store.dispatch is not a function' redux action testing with jest
[英]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.