簡體   English   中英

反應 - 無法增加數量/在購物車中添加多個項目

[英]React - unable to increase quantity / append multiple items in shopping cart

我遇到購物車問題。 無法增加購物車中現有商品的數量或添加其他商品。 在按鈕上單擊 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);
    }
  };

我只得到 1 個產品數量,如果我添加另一個產品或增加數量,它會覆蓋。

你的 else 邏輯是錯誤的。 您希望在傳播之前從 carItems 更新您的商品數量。 改變:

const updateCart = [...cartItems];
updateCart[index].quantity += 1;
setCartItems(updateCart);  

      cartItems[index].quantity += 1;
      const updateCart = [...cartItems];
      setCartItems(updateCart);  

編輯:請參閱下面的操作:

 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));

工作示例

夠好嗎? 不是為了反應

當您使用組件渲染購物車時,可能需要用新對象替換購物車中的項目 [-line]。 鐵:

cartItems.map( item => <CartOrderLine key={item._id} data={item} /> )

<CartOrderLine />具有相同的data對象 ref(僅限變異quantity道具,未替換)不會被重新渲染!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM