[英]When I click to increase the quantity of an item in cart, It increases two instead of one
I am facing an issue with the shopping cart.我遇到了购物车问题。 After clicking on INCREASE_QTY to increase the quantity of an existing item in the cart, It increases two instead of one.单击 INCREASE_QTY 以增加购物车中现有商品的数量后,它会增加两个而不是一个。 Total item quantity increase correctly but single item quantity now increases correctly.总项目数量正确增加,但单个项目数量现在正确增加。 I am using React useReducer hook with context API.我正在使用上下文 API 的 React useReducer 挂钩。
case "ADD_TO_CART":
if (state.cart.length === 5) {
return state;
} else {
items = action.item;
items["itemQty"] = 1; // add single item qty in array
updateQty = state.qty + 1; // total items qty
items["day"] = action.dayItem;
return {
cart: [items, ...state.cart],
qty: updateQty,
};
}
case "INCREMENT_QTY":
items = action.item;
items.itemQty = items.itemQty + 1
updateQty = state.qty + 1
index = state.cart.findIndex((cart) => cart.id === action.id);
state.cart[index] = items;
return {
cart: [...state.cart],
qty: updateQty,
}
I am getting quantity 2 after clicking once.单击一次后我得到数量 2。 How I can solve this issue我该如何解决这个问题
Take a look at the React docs here for how to implement a Reducer Hook with Context.在此处查看 React 文档,了解如何使用 Context 实现 Reducer Hook。
https://reactjs.org/docs/hooks-reference.html#usereducer (the React example from the docs outlines how to achieve your specific goal above. https://reactjs.org/docs/hooks-reference.html#usereducer (文档中的 React 示例概述了如何实现上述特定目标。
Here's another great resource for using context with a custom hook for Auth that may make the React pattern a little easier to understand at a higher level that helped me.这是另一个将上下文与 Auth 的自定义挂钩结合使用的重要资源,它可以使 React 模式在更高层次上更容易理解,这对我有帮助。 https://fatmali.medium.com/use-context-and-custom-hooks-to-share-user-state-across-your-react-app-ad7476baaf32 https://fatmali.medium.com/use-context-and-custom-hooks-to-share-user-state-across-your-react-app-ad7476baaf32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.