'how to add array to an object as property?, like object of more than 1 property and one of the property is array

enter image description here IMAGE 1

enter image description here IMAGE 2

enter image description here IMAGE 3

so initially in image 1 i have this function dispatch, when it gets called it passes two argument "type" and "item".

Now the problem is, in image 2 i need to return the item to state object and store it in that state and every time that dispatch function gets called new values of item should be added to state but but......the state object is not retaining the item value, it is removing the previous value and passing the new value so i am receiving only the last value.

I need to map that item property later and for that i need it to be an array, so it should be like:

const state = {
count: 1,
item: [product1, product2, product3, ......]
}

Note: product is an object



Solution 1:[1]

It should be something like

return {
  ...state,
  count : state.count + 1,
  item : [...state.item, action.item]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pierre