【问题标题】:redux-thunk and redux-saga simultaneouslyredux-thunk 和 redux-saga 同时
【发布时间】:2018-12-04 10:52:28
【问题描述】:

我遇到了两个资源,其中解释了如何一起使用两个中间件系统。

第一个说:

您可以在 thunk 中间件旁边添加 saga 中间件。请记住,您列出中间件的顺序很重要。

代码:

const store = createStore(reducer, applyMiddleware(thunk, sagaMiddleware))

第二个提供这部分代码source2

 createStore(rootReducer,applyMiddleware(sagaMiddleware, thunk)

没事吧?或者第一个只是提醒我们顺序很重要,但如果sagathunk 之间的顺序无关紧要?相对sagathunk,可能还有其他原因来警告订单?

【问题讨论】:

    标签: redux redux-thunk redux-saga


    【解决方案1】:

    是的,您绝对可以同时使用这两个中间件。

    顺序很重要,因为中间件管道顺序基于applyMiddleware() 的参数顺序。也就是说,当您有一个调用next(action) 的自定义中间件时,这主要是一个问题,它将操作转发到管道中的下一个中间件。对于 thunk 和 sagas,您通常会调用 dispatch(action),它总是从管道的开头开始。

    更多详情请见the Redux FAQ entry on "what is the difference between next and dispatch in a middleware?"

    【讨论】:

      【解决方案2】:

      你可以使用来自 saga api 的putResolve

      putResolve(action)
      Just like put but the effect is blocking (if promise is returned from dispatch it will wait for its resolution) and will bubble up errors from downstream.
      

      https://redux-saga.js.org/docs/api/ 由于 redux-thunks 返回一个 Promise,您可以直接在 sagas 中调用它们并等待返回值。 此外,它会冒出任何错误。

      【讨论】: