在createStore()之后添加增强器到redux存储

uni*_*nal 7 redux

这可能吗?

redux在IoC环境中使用存储,并希望在创建后将中间件添加到商店.

例如:

class MyApp {
  store = createStore(...);
}

let app = new MyApp();

// later on
import thunk from 'redux-thunk';
app.store.addEnhancer(thunk);
Run Code Online (Sandbox Code Playgroud)

uni*_*nal 3

我创建了一个函数来执行此操作。如果redux觉得这个有价值,我可以做个PR。

这是为我的模块量身定制的代码。添加到公关中的实际内容看起来会有点不同。

addMiddleware(middleware: Middleware) {
  const middlewareAPI: MiddlewareAPI<any> = {
    getState: this.getState,
    dispatch: (action) => this.dispatch(action)
  };

  this.dispatch = compose(middleware(middlewareAPI))(this.dispatch);
}
Run Code Online (Sandbox Code Playgroud)