【问题标题】:Is it possible to merge two reducers into one?是否可以将两个减速器合并为一个?
【发布时间】:2019-08-10 16:52:55
【问题描述】:

我想合并两个 reducer,第一个被创建为通用的,第二个更具体到它自己的状态。这两个减速器都不会处理相同的情况。合并这些只会导致默认情况被复制,默认情况总是返回默认状态。这会有所帮助,因为我只会测试一次通用的。

如果你在考虑reduceReducerscombineReducers,那是行不通的,因为我有许多“特殊”减速器,每个减速器都有相同的动作类型要处理,所有这些减速器都有不同的部分要修改的状态。

const initialState = {
  byId : {},
  ids: []
}

const dogsReducer = ({ dogs: state = initialState, ...restOfState }, action) => {
  switch (action.type) {
    case INITIALIZE:
      return {
        byId : _.keyBy(state.dogs, 'id'),
        ids: state.map(({id}) => id)
      }
    case RESET:
      return initialState
    case SPECIFIC_DOG_ACTION:
      ...
    default:
      return state
  }
}

const catsReducer = ({ cats: state = initialState, ...restOfState}, action) => {
  switch (action.type) {
    case INITIALIZE:
      return {
        byId : _.keyBy(state, 'id'),
        ids: state.map(({id}) => id)
      }
    case RESET:
      return initialState
    case SPECIFIC_CAT_ACTION:
      ...
    default:
      return state
  }
}

我想隔离以下情况:INITIALIZERESET 在通用 switch/case 函数或通用减速器中,所以我只需要测试这些情况一次,而不是在每个减速器中。以后会有更多的通用案例,所以我想避免重复。

这是预期的结果:

const genericReducer = (state = initialState, action) => {
  switch (action.type) {
    case INITIALIZE:
      return {
        byId : _.keyBy(state.dogs, 'id'),
        ids: state.map(({id}) => id)
      }
    case RESET:
      return initialState
    default:
      return state
   }
}

const dogsReducer = ({ dogs: state = initialState, ...restOfState }, action) => {
  switch (action.type) {
    case SPECIFIC_DOG_ACTION:
      ...
    default:
      return state
  }
}

const catsReducer = ({ cats: state = initialState, ...restOfState}, action) => {
  switch (action.type) {
    case SPECIFIC_CAT_ACTION:
      ...
    default:
      return state
  }
}

const finalCatsReducer = mergeReducers(catsReducer, genericReducer)
const finalDogsReducer = mergeReducers(dogsReducer, genericReducer)

【问题讨论】:

  • “用普通的 javascript 怎么做?” - 就像你刚才所做的一样 (fiddle)。那么实际的问题/错误是什么?
  • 什么对结果不起作用?
  • 这似乎是一件奇怪的事情。您可能会考虑描述这应该解决什么问题,因为可能有比编写一个通常组合 switch 语句的函数更好的方法来解决它。见x-y problem
  • 如果他们有不同的情况并且默认值相同,那么注释的单函数方法有什么问题?
  • 很好,但是您过度简化了整个概念,并且没有提供足够的实际用例来提供有效的答案。注意上面关于“你想解决什么问题”的问题

标签: javascript redux react-redux


【解决方案1】:

我可以想象使用以下内容,但是我想说这与将它们全部放平具有类似的效果。我能看到的唯一额外好处是,除非一般情况失败,否则不会验证特定的 switch case。

const genericSwitch = type => {
  switch (type) {
    case 1:
      do something x

    default: //specificSwitch
      switch (type) {
        case 2:
          do something y
      default:
          return z
      }
  }
}

【讨论】:

  • 问题是关于合并而不是加入。不是吗?
  • @Jackkobec 确实如此,但它输出的结果与要求的完全相同。
【解决方案2】:

最简单的解决方法是包裹到上层方法中:

const combinedSwitch = type => {
  const result = genericSwitch(type);
  return result === z ? specificSwitch(type) : result;
}

const genericSwitch = type => {
  switch (type) {
    case 1:
      do something x
    default:
      return z
  }
}

const specificSwitch = type => {
  switch (type) {
    case 2:
      do something y
    default:
      return z
  }
}

【讨论】:

  • 我会从特定的而不是通用的开始。非常感谢,这是一个简单易懂的替代方案。
猜你喜欢
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 2015-06-08
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多