How to update value of object in specific array item in redux

Issue

This Content is from Stack Overflow. Question asked by kamil danilski

cart
-attributes: [
{name: "size", value"},
{name: "color", value"}]

my actions:

     export const changeAttributes = (id, selectedAtribut) => ({
    type: CHANGE_ATTRIBUTES,
    id,
    payload: selectedAtribut 
  });

my reducers:

 case CHANGE_ATTRIBUTES: {
      const { name, value } = action.payload.selectedAtribut;
      return {
        ...state,
        cart: [
          ...state.cart.map((product) => {
            if (action.cartId !== product.cartId) {
              return product;
            }
            return {
              ...product,
             selectedAttributes: action.selectedAttributes.forEach((attribute) => {
                if (attribute.name === name) {
                  attribute.value = value;
                }
              })
            };
          }),
        ],
      }};

my dispatcher

  static updateAttributes(dispatch, product, attributes) {
       dispatch(updateAttributes(product, attributes));
  }

in parent I get changeAttributes from actions and give it to this.props.

<Component
itemCardID={itemCardID}
changeAttributes={changeAttributes}/>

This value I put as props to dispatchMap and into a child component.

In childComponent

<input 
..values,

              onChange={() => changeAttributes(itemCardID, this.event.target)} />



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?