【问题标题】:What is the correct way to combine redux-thunk and redux-batched-actions?结合 redux-thunk 和 redux-batched-actions 的正确方法是什么?
【发布时间】:2018-07-18 16:12:20
【问题描述】:

redux-batched-actions 插入我现有的 Redux 存储的正确方法是什么?我完全被 Redux 中间件 API 搞糊涂了。

目前我正在使用redux-thunkredux-little-router

这是创建我的 Redux 存储的代码源:

import { createStore, applyMiddleware, compose, combineReducers } from 'redux'
import thunk from 'redux-thunk'
import { routerForBrowser } from 'redux-little-router'
import reducers from './store'
import routes from './routes'

const { reducer, middleware, enhancer } = routerForBrowser({ routes })

// Combine all reducers and instantiate the app-wide store instance
const allReducers = combineReducers({ ...reducers, router: reducer })

// Build middleware (if devTools is installed, connect to it)
const allEnhancers = (window.__REDUX_DEVTOOLS_EXTENSION__
  ? compose(
      enhancer,
      applyMiddleware(thunk, middleware),
      window.__REDUX_DEVTOOLS_EXTENSION__())
  : compose(
      enhancer,
      applyMiddleware(thunk, middleware)))

// Instantiate the app-wide store instance
const initialState = window.initialReduxState
const store = createStore(
  allReducers,
  initialState,
  allEnhancers
)

redux-batched-actions documentation 公开了两种用法:enableBatchingbatchDispatchMiddleware。在我的情况下我应该使用哪一个?

【问题讨论】:

    标签: redux redux-thunk redux-batched-actions


    【解决方案1】:

    在我的探险返回到 redux、redux-thunk、redux-batched-actions 的精彩源代码后回答我自己的问题......

    正确的做法似乎是使用batchDispatchMiddleware,像这样:

    import { batchDispatchMiddleware } from 'redux-batched-actions'
    
    // ...
    
    const allEnhancers = (window.__REDUX_DEVTOOLS_EXTENSION__
      ? compose(
          enhancer,
          applyMiddleware(batchDispatchMiddleware, thunk, middleware),
          window.__REDUX_DEVTOOLS_EXTENSION__())
      : compose(
          enhancer,
          applyMiddleware(batchDispatchMiddleware, thunk, middleware)))
    

    注意:不过,我不知道我是否可以分批发送 thunk。我在当前的应用程序中没有这样做。使用风险自负!

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 2021-08-22
      • 2019-10-20
      • 2018-10-21
      • 2016-08-03
      • 2016-11-19
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多