创建商店后添加更多减速器

问题描述

创建商店后是否可以添加减速器?

示例: 假设我有这个React应用

//App.jsx
const reducers = {foo : []}
const store = createStore(combineReducers(reducers));

const App = () => <Provider store={store}><Test/></Provider>

//Test.jsx
class Test extends Component {

     componentDidMount() {
          //here I want to add new reducer to the store
          //I will handle duplication problem
          //I'm looking for something like this
          store.appendReducer('REDUCER_KEY',someReducer);  
     }
     ...
}
export default connect()(Test)

我想知道这是否可行,如果可以的话如何实现。

我有一个主意,但这并不完美。我可以添加一个全局回调函数,并在根组件上侦听任何调用,然后在发生任何调用时重新创建存储。这样的问题将导致在任何减速器添加时重新渲染整个组件树。

解决方法

是的,可能有类似的事情

const newRootReducer = combineReducers({
  existingSlice: existingSliceReducer,newSlice: newSliceReducer
})

store.replaceReducer(newRootReducer)

您可以检查DOC以获取更多信息redux DOC

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...