1

I am mutating state here, but I don't want to! All my attempts to not mutate state have come back with syntax errors, so I have turned here.

this is my redux data structure:

controls: (array)[
    0:
        id: "e06c5fbf-6d57-4f5b-a601-bfc4ad265def"
        status: 'complete'
        files: []
    1:
        id: "e06c5fbf-6d57-4f5b-a601-bfc4ad265def"
        status: 'complete'
        files: []
    ...
    ]

// and my reducer

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
2
  • what do you get as result?? Commented Jan 17, 2020 at 5:37
  • with my current code, it properly edits the desired status property, but it doesn't do it immutably. Commented Jan 17, 2020 at 5:38

2 Answers 2

1

This worked for me:

// reducer

    let index = action.payload.index
    let controls = [...state.controls];
    controls[index] = {...controls[index], status: action.payload.value};
    return {...state, controls}
Sign up to request clarification or add additional context in comments.

Comments

0

If you are finding and updating particular row/item.

           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
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.