Skip to content

Chapter 5 - CartReducer #12

@bttb

Description

@bttb

Hi Adam,

in Chapter 5 inside the CartReducer you have this:

        let existing = newStore.cart.find(item => item.product.id === p.id);
        if (existing) {
            existing.quantity += q;
        } else {
            newStore.cart = [...newStore.cart, action.payload];
        }

The problem is: If the item already exists in the cart, you change the quantity of the existing item. But: This changes storeData, because existing is inside storeData AND inside newStore. So this makes the function unpure, i.e. it is not what you want as a Reducer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions