[英]React - unable to increase quantity / append multiple items in shopping cart
I am facing an issue with shopping cart.我遇到购物车问题。 Unable to increase quantity of existing item in cart or add another item.
无法增加购物车中现有商品的数量或添加其他商品。 On button click the addToCart function executes which takes a product.
在按钮上单击 addToCart 函数执行它需要一个产品。
const [cartItems, setCartItems] = useState([])
const addToCart = product => {
console.log(product) // here I am getting the entire product
const index = cartItems.findIndex(item => item._id === product._id);
if (index === -1) {
const updateCart = cartItems.concat({
...product,
quantity: 1
});
setCartItems(updateCart);
} else {
const updateCart = [...cartItems];
updateCart[index].quantity += 1;
setCartItems(updateCart);
}
};
I am getting only 1 quantity of product and if I add another product or increase quantity, it overwrites.我只得到 1 个产品数量,如果我添加另一个产品或增加数量,它会覆盖。
Your else logic is wrong.你的 else 逻辑是错误的。 You want to update your item quantity from carItems, before you spread it.
您希望在传播之前从 carItems 更新您的商品数量。 Change:
改变:
const updateCart = [...cartItems];
updateCart[index].quantity += 1;
setCartItems(updateCart);
to到
cartItems[index].quantity += 1;
const updateCart = [...cartItems];
setCartItems(updateCart);
Edit: See it in action below:编辑:请参阅下面的操作:
let cartItems = [{ _id: "1", name: "shoe", quantity: 1 }]; const addToCart = product => { console.log(product) // here I am getting the entire product const index = cartItems.findIndex(item => item._id === product._id); if (index === -1) { cartItems.push({ ...product, quantity: 1 }); const updateCart = [...cartItems]; console.log(updateCart); } else { cartItems[index].quantity += 1; const updateCart = [...cartItems]; console.log(updateCart); } }; var product1 = { _id: "1", name: "shoe" }; var product2 = { _id: "2", name: "apple" }; addToCart(product1); addToCart(product2);
let cartItems = [ { _id: "1", name: "shoe", quantity: 1 } ]; console.log("START", JSON.stringify(cartItems), Array.isArray(cartItems)); const addToCart = product => { console.log(product); // here I am getting the entire product const index = cartItems.findIndex(item => item._id === product._id); if (index === -1) { const updateCart = cartItems.concat({ ...product, quantity: 1 }); cartItems = updateCart; console.log("ADD", JSON.stringify(cartItems), Array.isArray(cartItems)); } else { // original ... just works ... // const updateCart = [...cartItems]; // updateCart[index].quantity += 1; // ... this works, too // cartItems[index].quantity += 1; // const updateCart = [...cartItems]; // ... but this is safer (in react) as it replaces item with a new object const updateCart = [...cartItems]; // new object for replace existing one updateCart[index] = { ...updateCart[index], quantity: updateCart[index].quantity + 1 }; cartItems = updateCart; console.log("INC", JSON.stringify(cartItems), Array.isArray(cartItems)); } }; var product1 = { _id: "1", name: "shoe" }; var product2 = { _id: "2", name: "apple" }; addToCart(product1); addToCart(product2); addToCart(product1); console.log("END", JSON.stringify(cartItems), Array.isArray(cartItems));
Replacing item [-line] in cart with a new object can be required in react when you're rendering cart using components.当您使用组件渲染购物车时,可能需要用新对象替换购物车中的项目 [-line]。 fe:
铁:
cartItems.map( item => <CartOrderLine key={item._id} data={item} /> )
<CartOrderLine />
with the same data
object ref (mutated quantity
prop only, not replaced) won't be rerendered! <CartOrderLine />
具有相同的data
对象 ref(仅限变异quantity
道具,未替换)不会被重新渲染!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.