You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改react-boilerplate:将注入的reducers嵌套到store单个键下

How to Nest Injected Reducers Under a Single Key in react-boilerplate

Got it, let's sort this out so your container-specific reducers live neatly under a dedicated containers key instead of cluttering up the root state. Here's a step-by-step breakdown:

1. Modify the Root Reducer Setup

First, head to your src/reducers.js file. The original createReducer function spreads injected reducers directly into the root state—we'll adjust it to nest those reducers under a containers key using combineReducers again.

Before:

import { combineReducers } from 'redux';
import routeReducer from './route';
import languageProviderReducer from './languageProvider';

export function createReducer(injectedReducers = {}) {
  return combineReducers({
    route: routeReducer,
    language: languageProviderReducer,
    ...injectedReducers, // Dumps container reducers directly into root state
  });
}

After:

import { combineReducers } from 'redux';
import routeReducer from './route';
import languageProviderReducer from './languageProvider';

export function createReducer(injectedReducers = {}) {
  return combineReducers({
    route: routeReducer,
    language: languageProviderReducer,
    // Nest all injected container reducers under the "containers" key
    containers: combineReducers(injectedReducers),
  });
}

This changes your root state structure to:

{
  route: routeReducerState,
  language: languageProviderReducerState,
  containers: {
    container1: container1ReducerState,
    container2: container2ReducerState,
    container3: container3ReducerState,
  }
}

2. Update State Access in Components & Selectors

Any component or selector that was pulling state directly from the root needs to adjust its path to look inside containers.

Example: Before (in a container component)

const mapStateToProps = state => ({
  userData: state.container1.userData,
});

After:

const mapStateToProps = state => ({
  userData: state.containers.container1.userData,
});

Don’t forget to check your reselect selectors too—if any reference root-level container state, update those paths as well.

3. Verify Dynamic Injection & Hot Reloading

react-boilerplate’s injectReducer utility works by adding reducers to the injectedReducers object, which our updated createReducer now wraps into the containers key. Test that dynamic injection still works (e.g., load a lazy-loaded container and confirm its reducer shows up under state.containers).

Also, double-check hot reloading for reducers—make sure changes to container reducers reflect in the store without breaking the app.

Why This Helps

  • Cleaner root state: Separates global app state (route, language) from container-specific state
  • Avoids naming collisions: No more stress about a container's reducer name clashing with a global reducer
  • Better organization: Groups all container state in one predictable spot

内容的提问来源于stack exchange,提问作者tacos_tacos_tacos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:02:56