You need to pass productIndex or id along with the imageFileName to correctly do the delete. Otherwise, all the products having the same imageFileName will be deleted.
Try this
case appConstants.DELETE_IMAGE_SUCCESS:
return {
...state,
products: (state.products || []).map((item, itemIndex) => {
if (itemIndex === action.payload.productIndex) {
return {
...item,
productImages: item.productImages.filter(
(image) => image.imageFileName !== action.payload.imageFileName
)
};
}
return item;
})
};
In App.js file
const onDeleteImage = (productIndex, imageFileName) => {
dispatch(deleteImage(productIndex, imageFileName));
};
in actions index.js file
export const deleteImage = (productIndex, imageFileName) => {
return {
type: appConstants.DELETE_IMAGE_SUCCESS,
payload: {
productIndex,
imageFileName
}
};
};
In MediaCard.js file
<Button
type="button"
color="primary"
variant="contained"
onClick={() => onDeleteImage(productIndex, data.imageFileName)}
>
Code sandbox => https://codesandbox.io/s/redux-added-array-of-object-inside-another-aray-in-react-forked-xmghr?file=/src/reducers/productReducer.js