【问题标题】:Accessing a nested key inside reducer访问 reducer 中的嵌套键
【发布时间】:2018-07-16 19:45:49
【问题描述】:

我很难说出这个问题。但是如何访问 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}

结果是他们无法定义 state.appointment

【问题讨论】:

  • 状态最初包含什么?你要派什么?调度后你想要什么结果?

标签: javascript reactjs redux


【解决方案1】:

这应该可以。您可以通过在减速器 initialState 中添加 appointment 的默认值来删除 state && state.appointment 真实检查:

const initialState = {
  appointment: 'A'
};

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多