不可避免地更新对象中的对象数组

问题描述 投票:1回答:2

我在这里改变状态,但是我不想!我所有不改变状态的尝试都返回了语法错误,因此我转向这里。

这是我的redux数据结构:

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
reactjs redux immutability mutable
2个回答
0
投票

这对我有用:

//减速器

    let index = action.payload.index
    let controls = [...state.controls];
    controls[index] = {...controls[index], status: action.payload.value};
    return {...state, controls}

0
投票

如果您正在查找和更新特定的行/项目。

           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
            }
© www.soinside.com 2019 - 2024. All rights reserved.