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,
}
},
}
}
你想实现什么?你想实现什么?很有魅力!谢谢:)很有魅力!谢谢:)