在父减速器的默认块中调用sub-reducer

时间:2017-09-25 15:30:15

标签: redux react-redux

是否可以让减速器在其默认块中调用子减速器?

function aReducer(state = {}, action) {
  switch(action.type) {
    case XYZ:
      ... // know what to do
    default:
      // don't know this action, let's delegate to the children
      return {
        sub1: subReducer1(state.sub1, action),
        sub2: subReducer2(state.sub2, action)
        }
  }
}

2 个答案:

答案 0 :(得分:1)

是的,这绝对合法且合理。

您可能需要阅读Redux docs section on "Structuring Reducers"以了解有关如何组织减速器逻辑的更多建议。

答案 1 :(得分:0)

您可以将所有Reducer放在一个公共文件夹中,在其中,您可以将单独的Reducer组合成一个,如下面的代码。

import { combineReducers } from 'redux'
import Reducer1 from './Reducer1.js'
import Reducer2 from './Reducer2.js'
export default combineReducers( { Reducer1, Reducer2,... } )

并使用以下代码将其用作单个reducer。

import reducers from '../../reducers'(reducer's root folder name/path)
let store = createStore( reducers );