Items issue

So basically i finished my project but i have a bug if i have an item and like i double press the cart it adds the item to two places , i want only to add the quantity i have done like in my cart the plus and minus for adding quantity but here is my code addToCart: (state, action) => { const itemInCart = state.cart.find((item) => item.id === action.payload.id); if (itemInCart === action.payload && itemInCart === 1) { itemInCart.quantity++; } else if (itemInCart) { itemInCart.quantity++; } else { state.cart.push({...action.payload, quantity: 1 }); } },
16 Replies
anes039
anes039OP4mo ago
i used redux here also i tried this ItemIncart.quantity === 1 , but nothing changed
Jeluxe
Jeluxe4mo ago
Just wandering why are checking if itemInCart equals to action.payload if you already check equation of the id within the find method, Also it does not seem like you update the quantity in the state but i am not sure if it is related to the issue
anes039
anes039OP4mo ago
addToCart: (state, action) => { const itemInCart = state.cart.find((item) => item.id === action.payload.id); if (itemInCart && itemInCart.quantity === 1) { itemInCart.quantity++; } else if (itemInCart) { itemInCart.quantity++; } else { state.cart.push({...action.payload, quantity: 1 }); } }, look incrementQuantity: (state, action) => { const item = state.cart.find((item) => item.id === action.payload); item.quantity++; }, @Jeluxe any idea?
Jeluxe
Jeluxe4mo ago
so what im trying to understand where do you update the state after you increase the itemInCart quantity when you add item to the cart
anes039
anes039OP4mo ago
i have an icon cart icon on my main page when i click it inside the item card , the item it is added in the cart, but the issue is if i double press it i got two same items in a cart and they are like selected as different because the quantity is not raising as it should , but there i have + and - and i can add quantity or remove should i send the code here? maybe is it easier for u?
Jeluxe
Jeluxe4mo ago
Are you using redux or redux toolkit?
anes039
anes039OP4mo ago
i think redux
Jeluxe
Jeluxe4mo ago
First, for your issue with double press, there is something called debounce, which supposed help with that issue.
anes039
anes039OP4mo ago
okay let me see
Jeluxe
Jeluxe4mo ago
And here i meant where is the state updating inside addToCart function And i also think that if it pushes the item as another item and does not increase the quantity, then it means that it is getting to the else which indicates that itemInCart does not exists, i assuming...
anes039
anes039OP4mo ago
lmao do u know where was the error wait const DummyData = [ { img: Barca, id:1, team: 'Barcelona Outfit 23/24', price: '$64.95', }, {
img: 'https://i.pinimg.com/236x/a0/4f/c9/a04fc97d7252af13d3068be66f54428d.jpg', id: 2, team: 'Bayern Outfit 23/24', price: '$99.99', }, { img: City, id: 3, team: 'Manchester City Outfit 23/24', price: '$75.99', }, { img: 'https://i.pinimg.com/474x/f4/25/78/f425785b23f0587721d2bc4b81cf2c68.jpg', id: 4, team: 'Real Madrid Outfit 23/24', price: '$94.99', }, { img: Inter, id: 5, team: 'Internazionale Outfit 23/24', price: '$54.99', }, { img: 'https://i.pinimg.com/236x/21/0f/45/210f457330f2bee5f5301a7a3f49115c.jpg', id: 6, team: 'Arsenal Outfit 23/24', price: '$63.59', } ]; you see this? at id i used Math.random() every time i added the item it used to give me a random Id so thats why i got like the same item twice addToCart: (state, action) => { const itemInCart = state.cart.find((item) => item.id === action.payload.id)
if (itemInCart) { itemInCart.quantity++; } else { state.cart.push({...action.payload, quantity: 1 }); } }, and i set my function like this lmao
Jeluxe
Jeluxe4mo ago
So now is it working?
anes039
anes039OP4mo ago
yes bro the problem was with the id because it did not match each time i added it generated me a random one you know
Jeluxe
Jeluxe4mo ago
Yes xd
anes039
anes039OP4mo ago
thanks for your effort bro btw god bless
Jeluxe
Jeluxe4mo ago
np good luck
Want results from more Discord servers?
Add your server