重用reducer逻辑,动作名称未定义

时间:2019-03-14 16:41:59

标签: javascript reactjs redux react-redux reducers

我正在看以下文档页面:this thread在redux上。我正在尝试实现这样的东西:

function counter(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
}

function createNamedWrapperReducer(reducerFunction, reducerName) {
  return (state, action) => {
    const { name } = action
    const isInitializationCall = state === undefined
    if (name !== reducerName && !isInitializationCall) return state

    return reducerFunction(state, action)
  }
}

const rootReducer = combineReducers({
  counterA: createNamedWrapperReducer(counter, 'A'),
  counterB: createNamedWrapperReducer(counter, 'B'),
  counterC: createNamedWrapperReducer(counter, 'C')
})

但是我不明白如何从action中获取名字。而且此代码不适用于我的项目,因为const { name } = action返回了undefined

我想念的是什么?这个例子完成了吗?我该如何解决?

0 个答案:

没有答案