I
I
Ivan Shulga2020-05-27 18:07:44
redux
Ivan Shulga, 2020-05-27 18:07:44

How to correctly change the property of an array element?

How to do it correctly to copy the state, but at the same time change active to the opposite one on the incoming id?

const CHECKED_POST = 'CHECKED_POST'

let initialState = {
    checkboxes: [
        {id: 1, message: 'Встать рано утром', active: false},
        {id: 2, message: 'Скушать вкусный завтрак', active: false},
        {id: 3, message: 'Провести тренировку', active: false},
        {id: 4, message: 'Поработать', active: false},
        {id: 5, message: 'Встретиться с друзьями', active: false}
    ]
}

const todoReducer = (state= initialState, action) => {

    switch (action.type) {
        case CHECKED_POST: {
            return {
                ...state,
                checkboxes: [...state.checkboxes.filter((id) => {
                    if (id !== action.id) {
                        return {
                               //Как правильно?
                        }
                    }
                })]
            }
        }

        default:
            return state
    }
}

export const checkedPost = (id) => (dispatch) => dispatch({type: CHECKED_POST}, id)


export default todoReducer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-05-27
@ZamarShoo

state.checkboxes.map(n => n.id === action.id
  ? { ...n, active: !n.active }
  : n
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question