【问题标题】:redux-offline ignore middlewares when executing commit or rollback actionsredux-offline 在执行提交或回滚操作时忽略中间件
【发布时间】:2018-07-30 09:16:36
【问题描述】:

https://github.com/redux-offline/redux-offline/pull/178#issuecomment-408795302 所示,我们正在尝试与redux-offline 一起使用一个中间件,该中间件可以在它们的对应部分commitrollback 执行后调度新动作。重点是这些都没有被调度,调试之后发现,在调度初始动作时,中间件被用作dispatch()函数(可能是由于reduxcomposeEnhancers()applyMiddleware()函数的工作原理,因为它们链接了函数),但是当 commit 动作被调度时,它是直接使用 store dispatch() 方法完成的,所以根本没有执行任何中间件。

我们不能完全确定是我们这边的redux-offline 配置错误,还是redux-offline 本身的错误...我们的商店配置是这样的:

import { applyMiddleware, compose, createStore } from 'redux'

import reduxOfflineThunkMiddleware from './thunk-middleware'
import rootReducer from '../../reducers'

import { createUser } from '../../actions/user'

const initialState = {}

const windowCompose = (typeof window !== 'undefined') && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
const composeEnhancers = windowCompose || compose

const store = createStore(
  rootReducer,
  initialState,
  composeEnhancers(
    applyMiddleware(reduxOfflineThunkMiddleware({ createUser })),
    offline()
  )
)

【问题讨论】:

    标签: redux middleware redux-offline


    【解决方案1】:

    是的,offlineapplyMiddleware 都是“商店增强器”。当您调用store.dispatch 时,操作顺序将是:

    • 由中间件管道中的所有中间件处理
    • offline处理
    • 由商店自行处理

    因为offline 增强器在applyMiddleware 增强器之后,所以它在内部调度的任何操作都不会通过中间件管道。

    【讨论】:

    • 谢谢@markerikson,您解决了问题,为我们节省了大量时间。
    • 好的,我们刚刚将offline() 调用放在applyMiddleware() 之前,它就起作用了。我只是忘记了它可以作为堆栈工作,实际上文档说store enhancers are applied right to left,谢谢:-)
    • 这对我有用。特别是你的评论@Piranna。谢谢!
    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2019-06-05
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2020-05-13
    相关资源
    最近更新 更多