Skip to content

Fix for cart reordering while adding quantity.#92

Open
ashharr wants to merge 1 commit into
adrianhajdin:mainfrom
ashharr:main
Open

Fix for cart reordering while adding quantity.#92
ashharr wants to merge 1 commit into
adrianhajdin:mainfrom
ashharr:main

Conversation

@ashharr

@ashharr ashharr commented Jan 4, 2023

Copy link
Copy Markdown

Create a copy of the cartItems:
const newCartItems = [...cartItems]; // for mutating the array in place

now we increase quantity, in the foundProduct and simply add it in the new array using index to prevent reordering.

    if(value === 'inc') {
      foundProduct.quantity += 1;
      newCartItems[index] = foundProduct;  // prevents reordering
      setCartItems(newCartItems)

@graphiiics

Copy link
Copy Markdown

Very nice approach!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants