0

I know there is a lot of similar threads but I didn't find a solution there.

Okay so I have a state which looks:

[
{
    id: 1,
    title: 'todo',
    items: [
        {
            itemID: 1,
            itemText: 'finish this project'
        },
        {
            itemID: 2,
            itemText: 'take trash out'
        },
        {
            itemID: 3,
            itemText: 'sleep'
        },

    ]
},
{
    id: 2,
    title: 'doing now',
    items: [
        {
            itemID: 1,
            itemText: 'add redux'
        },
        {
            itemID: 2,
            itemText: 'fixing bugs'
        },
    ]
},
{
    id: 3,
    title: 'done',
    items: [
        {
            itemID: 1,
            itemText: 'add components'
        },
        {
            itemID: 2,
            itemText: 'add material ui'
        },
    ]
}

]

and I have a redux reducer that should add an item into specific list, I have id of that list, and itemText which I should add. I managed somehow to make it kinda work, it is adding item into specifin array but problem is that also creates new blank list. Here is how reducer looks:

case "ADD_ITEM":
  return [
    state.map((list)=>(
    (list.id === action.payload.id) ?  list.items.push({
      itemID: 89,
      itemText: action.payload.description
    }) : list
    
  ))
  ]

Any idea how to add an item to specific list items without creating a new list?

2 Answers 2

2

Map and return the lists (state), and use spread syntax to update the items:

case "ADD_ITEM":
  return state.map(list =>
    list.id === action.payload.id ? {
      ...list,
      items: [
        ...list.items,
        {
          itemID: 89,
          itemText: action.payload.description
        }
      ]
    } : list
  )
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what I was looking for, thank you!
Sorry for bothering again but I got stuck again haha, now I m trying to remove item, this is how I m doing it but nothing is happening ``` case "REMOVE_ITEM": return state.map((list) => list.id === action.payload.listID ? { ...list, items: list.items.filter(item => item.itemID !== action.payload.itemID) } : list) ```
To remove items use filter instead of map, and keep all items which id is no identical to the one you wish to remove.
0

Try this:

case "ADD_ITEM":
    state.forEach((list, index)=>(
    if (list.id === action.payload.id){
      state[index].items.push({
        itemID: 89,
        itemText: action.payload.description
      }
    })
    
  ))

Note that this is pseudocode, but it should still work.

Your example was not working because you were returning a new array, which had (about) the same structure as state, except with the added element. To add it, simply add it directly to state without using map + return, thereby not creating a new array.

1 Comment

Thanks for explaining now I understand why it worked in that way, anyway Ori's answer is what I was looking for.

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.