Reactjs 在redux中不可变地更新对象数组
我正在变异状态,但我不想!我所有不改变状态的尝试都出现了语法错误,所以我转到这里 这是我的redux数据结构:Reactjs 在redux中不可变地更新对象数组,reactjs,redux,immutability,mutable,Reactjs,Redux,Immutability,Mutable,我正在变异状态,但我不想!我所有不改变状态的尝试都出现了语法错误,所以我转到这里 这是我的redux数据结构: controls: (array)[ 0: id: "e06c5fbf-6d57-4f5b-a601-bfc4ad265def" status: 'complete' files: [] 1: id: "e06c5fbf-6d57-4f5b-a601-bfc4ad265def" status
controls: (array)[
0:
id: "e06c5fbf-6d57-4f5b-a601-bfc4ad265def"
status: 'complete'
files: []
1:
id: "e06c5fbf-6d57-4f5b-a601-bfc4ad265def"
status: 'complete'
files: []
...
]
//我的减速机呢
export default function frameworkReducer(state = initialState, action) {
case RECEIVE_CONTROL_STATUS_UPDATE:
const index = action.payload.index;
const newState = {...state};
newState.controls[index] = { ...state.controls[index], status: action.payload.value };
return newState
这对我很有用:
//减速器
let index = action.payload.index
let controls = [...state.controls];
controls[index] = {...controls[index], status: action.payload.value};
return {...state, controls}
如果您正在查找和更新特定行/项目
case RECEIVE_CONTROL_STATUS_UPDATE:
let findItem = state.controls.find(a=>a.index===action.payload.index);
let filtered = state.controls.filter(a=>a.index!==action.payload.index);
let updateState = [filtered, findItem];
return {
...state,
constrols: updatedState
}
您得到了什么结果?使用我当前的代码,它正确地编辑了所需的status属性,但并不是一成不变的。