访问减速器中的嵌套键

时间:2018-07-16 19:45:50

标签: javascript reactjs redux

我很难说出这个问题。但是如何访问Redux中的嵌套键?我想将散布运算符应用于“ FETCH_APPOINTMENT”中的键“约会”,但是我不知道如何访问它。谢谢

我尝试了什么

case 'FETCH_APPOINTMENT':
  return { ...state, appointment: action.payload.data };

case 'SOME_CASE':
  return {
    [state && state.appointment]: state && state.appointment,
    ..._.mapKeys(action.payload.data, 'id'),
  };
  result
  {undefined: undefined}

结果是他们无法定义状态。约会

1 个答案:

答案 0 :(得分:1)

这应该有效。您可以通过在化简器state && state.appointment中为appointment添加默认值来删除initialState真实性检查:

const initialState = {
  appointment: 'A'
};

const reducers = (state = initialState, action) => {
  switch(action) {
    case "SOME_CASE": {
      return {
        [state.appointment]: {
          ...[state.appointment],
          _.mapKeys(action.payload.data, 'id')
        }
      }
    }
  }
}