Nested Array in Redux Resetting Value in React

Viewed 41

I have an array called products and inside of that I have a field called productCategory. My problem is that its clearing up other values of the array.

Codesandboxis here CLICK HERE

case appConstants.CHANGE_PRODUCT_CATEGORY:
  return {
    ...state,
    products: state.products.map((product) => {
      product?.productCode === action.payload?.productCode
        ? {
            ...product,
            productCategory: action.payload.productCategory
          }
        : product;
    })
  };
1 Answers

Maybe it's because you have missed return keyword inside map function.

case appConstants.CHANGE_PRODUCT_CATEGORY:
    return {
    ...state,
    products: state.products.map((product) => {
      return product?.productCode === action.payload?.productCode
        ? {
            ...product,
            productCategory: action.payload.productCategory
          }
        : product;
    })
};
Related