[英]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.