A
A
animalbtw2021-09-23 10:36:53
JavaScript
animalbtw, 2021-09-23 10:36:53

How to add id to lists array in redux?

On each call, I would like to add the string id , obtained from action.payload , to the lists array. This code creates a new lists array when called, thus overwriting the value that was already there:

const initialUserState = {
    borders:[{ title: '', boardId: '1' ,lists: [] }]
}
export const boardsReducer = (state = initialUserState, action) => {
    switch (action.type)
    {
        case 'ADD_LIST':
            const {boardId, id} = action.payload
            const board = state[boardId]
            return { 
                      ...state,
                      [boardId]: { ...board, ['lists']: [id] }
                   }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-09-23
@animalbtw

case 'ADD_LIST':
  const { boardId, id } = action.payload;
  return {
    ...state,
    borders: state.borders.map(n => n.boardId === boardId
      ? { ...n, lists: [ ...n.lists, id ] }
      : n
    ),
  };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question