在Redux中间件中调用调度

cyb*_*bat 1 reactjs redux

我正在尝试在现有的reudx API中集成redux-simple-auth。我的旧实现使用本机,fetch并添加了标头等。此新模块提供了访存替换功能,但它返回了redux操作,并且在弄清楚如何进行设置方面有些费劲。

我的商店:

function configureStore (initialState) {
  const middlewares = [
    authMiddleware,  // This is from rediux-simple-auth
    apiMiddleware, // My own middleware
    thunk

  ]

  const store = createStore(rootReducer, initialState, compose(
    applyMiddleware(...middlewares), getInitialAuthState({ storage }))
  )
}
Run Code Online (Sandbox Code Playgroud)

我的中间件简化了:

import { fetch } from 'redux-simple-auth'
export default store => next => action => {      
  ...
  next(my start action...)
  return store.dispatch(fetch(API_ROOT + endpoint, { method }))  
    .then(response => {
       next(my success/fail action...)
    })

}
Run Code Online (Sandbox Code Playgroud)

当我运行此命令时,我可以在redux检查器中看到我的启动和失败操作,但不能看到获取操作(它确实触发了FETCH操作)

如果我打电话next而不是打电话,store.dispatch那么它在某种意义上会触发动作,但不会返回承诺,我无法获得结果。

我该如何解决此流程?

Syl*_*ain 6

next不是dispatch。尝试这个:

export default ({dispatch}) => next => action => ...
Run Code Online (Sandbox Code Playgroud)

使用dispatch派遣你的新动作和使用next(action)要通过原始动作到下一个中间件。