修改反应样板以将注入的减速器嵌套在商店的单个键下

时间:2018-03-29 20:20:55

标签: javascript reactjs redux react-boilerplate

react-boilerplate有一个实用程序injectReducer,用于允许将reducer异步附加到store,并在整个应用程序的reducers.js文件中{ {1}}将这些createReducer展开到状态中,以便您拥有如下状态:

injectedReducers

我希望将这些特定于容器的reducer扩展到全局状态的子键,如下所示:

{
  route: routeReducer,
  language: languageProviderReducer,
  container1: container1Reducer,
  container2: container2Reducer,
  container3: container3Reducer,
}

我天真地尝试修改{ route: routeReducer, language: languageProviderReducer, ui: { container1: container1Reducer, container2: container2Reducer, container3: container3Reducer, } } 方法reducers.js,如下所示:

createReducer

或者也许:

export default function createReducer(injectedReducers) {
  return combineReducers({
    route: routeReducer,
    language: languageProviderReducer,
    ui: (injectedReducers) ? combineReducers(injectedReducers) : ((state = {}) => state),
  });
}

但是现在当我尝试加载任何容器时,我收到一个错误:export default function createReducer(injectedReducers) { return combineReducers({ route: routeReducer, language: languageProviderReducer, ui: combineReducers(injectedReducers || {}), }); }

使用注入的Reducer和TypeError: Cannot read property '_currentElement' of null实现我想要的状态形状的最简单途径是什么?

1 个答案:

答案 0 :(得分:0)

我相信对我来说,密钥是仅在需要时(当存在至少一个ui时)添加injectedReducers密钥。

/**
 * Creates the main reducer with the dynamically injected ones
 */
export default function createReducer(injectedReducers) {
  const reducersToCombine = {
    form: formReducer,
    language: languageProviderReducer,
    route: routeReducer,
  };

  if (injectedReducers && Object.keys(injectedReducers).length > 0) {
    reducersToCombine.ui = combineReducers(injectedReducers);
  }

  return combineReducers(reducersToCombine);
}