![](/img/trans.png)
[英]how does dispatch, next, and the ordering of middleware work in react redux?
[英]how does the flow of a react redux middleware work?
笨拙的中間件在哪里出現? 這是我現在看到的中間件的順序,但是我陷入了Thunk進入的位置:
promise
值設置為true的操作 applyMiddlware
獲取存儲並返回一個函數。 商店
const store = createStore(
rootReducer,
applyMiddleware(thunkMiddleware, promiseErrorMiddleware, dataTrafficMiddleware)
)
actionCreator
dispatch({url: requestURL, promise: true})
promiseErrorMiddleware和dataTrafficMiddleware
const promiseErrorMiddleware = function (store) { //where is store from, applyMiddleware?
return function (next) { //where is next from?
return function (action) {
if (!action.promise) {
return next(action)
} else {
return fetch(action.url).then(function (data) {
...
next({data, needDirection: true})
})
}
}
}
}
const dataTrafficMiddleware = function (store) {
return function (next) {
return function (action) {
if (!action.needDirection) {
return next(action)
} else {
//dispatch regular action with type and data
}
}
}
}
}
要記住的一件事是中間件是鏈接在一起的。 當您調用next()
它將按照您在applyMiddleware()
定義它們的順序轉到下一個中間件。 在中間件鏈的末尾,操作通過化簡器進行。 在您的代碼中,該函數永遠不會通過thunk
中間件傳遞(因為thunk
在promiseErrorMiddleware
之前)。 另一方面,如果您dispatch()
操作,則該操作將從頭開始貫穿整個中間件鏈。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.