Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在对象数组中切换的Redux状态属性_Reactjs_Ecmascript 6_Redux - Fatal编程技术网

Reactjs 在对象数组中切换的Redux状态属性

Reactjs 在对象数组中切换的Redux状态属性,reactjs,ecmascript-6,redux,Reactjs,Ecmascript 6,Redux,我的目标如下: { "1": {"stepNumber": 1, "isActive": false}, "2": {"stepNumber": 2, "isActive": true}, "3": {"stepNumber": 3, "isActive": true}, "4": {"stepNumber": 4, "isActive": false} } 在我的减速机中,我正在发送一个动作TOGGLE\u ACTIVE\u STEP,该动作的有效载荷为stepNumber,

我的目标如下:

{
  "1": {"stepNumber": 1, "isActive": false},
  "2": {"stepNumber": 2, "isActive": true},
  "3": {"stepNumber": 3, "isActive": true},
  "4": {"stepNumber": 4, "isActive": false}
}
在我的减速机中,我正在发送一个动作
TOGGLE\u ACTIVE\u STEP
,该动作的有效载荷为
stepNumber
,例如
1
2
3

这是我目前的减速机:

case TOGGLE_ACTIVE_STEP: {
  state.processSteps[action.data.stepNumber.toString()].isActive =
  !state.processSteps[action.data.stepNumber.toString()].isActive;

  console.log(JSON.stringify(state.processSteps));

  return {
    ...state,
    processSteps: state.processSteps,
  };
}
这是一个相当混乱的解决方案,我知道以我的方式设置
状态
不是最佳实践,因为您应该以这种方式重新分配参数


执行相同功能的最佳方式是什么?

像传播
状态一样继续传播道具:

case TOGGLE_ACTIVE_STEP: {
  return {
    ...state,
    processSteps: {
      ...state.processSteps,
      [action.data.stepNumber]: {
        ...state.processSteps[action.data.stepNumber],
        isActive: !state.processSteps[action.data.stepNumber].isActive,
      }
    },
  }
}

继续传播道具,就像传播
状态一样:

case TOGGLE_ACTIVE_STEP: {
  return {
    ...state,
    processSteps: {
      ...state.processSteps,
      [action.data.stepNumber]: {
        ...state.processSteps[action.data.stepNumber],
        isActive: !state.processSteps[action.data.stepNumber].isActive,
      }
    },
  }
}

你想实现什么?你想实现什么?很有魅力!谢谢:)很有魅力!谢谢:)