DEV Community

Discussion on: Using React Hooks to Make an RPG Shop - Part 2

Collapse
 
chron profile image
Paul Prestidge

About your reducer running twice – I think that's normal, but it shouldn't be a problem because reducers are supposed to be pure functions (with no side effects), which means running it twice should always produce the same result.

It looks like you are accidentally mutating the existing state in cartHelpers.js:

export const updateQuantity = (cart, item, quantity) => (
  cart.map(i => (
    i.name === item.name ?
      { ...i, quantity: i.quantity += quantity } : i
  ))
);
Enter fullscreen mode Exit fullscreen mode

I think that += should actually just be + and then you won't have any problems.

I also noticed this line in Store.js which I think will actually never return true:

const itemExists = cart.find(i => i.name === item);
Enter fullscreen mode Exit fullscreen mode

It looks like it should be comparing i.name to item.name, although obviously the code works either way since you have written your addToCart as an add / update depending on the current cart. But maybe something to refactor :)

Collapse
 
robotspacefish profile image
Jess

ahh good catches, thanks for the second set of eyes! :)