【问题标题】:How to increase quantity of the item if it is already added in shopping cart in React如果已在 React 的购物车中添加商品,如何增加商品的数量
【发布时间】:2020-06-17 12:49:46
【问题描述】:

我遇到了购物车问题。无法增加购物车中现有商品的数量或添加其他商品。在按钮上单击 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 件产品,如果我添加其他产品或增加数量,它会覆盖。

【问题讨论】:

标签: javascript reactjs react-native shopping-cart


【解决方案1】:

您的代码应该可以运行,非常好

  • 可能测试方法失败
  • 问题与代码的不同部分有关

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

working example

够好吗?不适用于反应

在使用组件渲染购物车时,可能需要将购物车中的项目 [-line] 替换为新对象。 f.e.:

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

&lt;CartOrderLine /&gt; 具有相同的 data 对象引用(仅更改 quantity 属性,未替换)不会被重新渲染!

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 没有真正的区别......应该替换项目对象
    • @xadm,如果购物车中有相同的商品,我们将对其进行变异
    • @FarhanFarooq,如果存在类似项目,您的逻辑是覆盖旧的......不是吗?
    • @SunilLama,仍然无法覆盖旧的
    • @SunilLama 如果 item mutated 渲染 item 组件获得相同的 ref [as prop],则没有视图更新
    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多