简体   繁体   English

如何在reactjs中多次使用一个function?

[英]How to use one function multiple times in reactjs?

I'm trying to create quantity section with reactjs but this is working only for one section.我正在尝试使用 reactjs 创建数量部分,但这仅适用于一个部分。 How can I make it multiple.我怎样才能让它成为多个。

When I'm click on up arrow then count is increasing same in all the {qty}.当我点击向上箭头时,所有 {qty} 的计数都在增加。 but I need when I'll click on up arrow then it should be count as per the products.但我需要什么时候点击向上箭头然后它应该根据产品进行计数。

Problem:- count is coming same:问题:- 计数相同: 在此处输入图像描述

Solution:- count should not same it should be like given below:解决方案:- 计数不应该相同,应该如下所示: 在此处输入图像描述

My Code:-我的代码:-

 const BuyProducts = () => { const title = 'Buy Products New'; const classes = productsStyles(); const [qty, setQty] = useState(0); const quantityMinus = () => { if (;qty == 0) { setQty(qty - 1); } }; const quantityPlus = () => { setQty(qty + 1); }; return ( <div> <div> <div onClick={quantityPlus} className="qty-action"> <FontAwesomeIcon icon={faAngleUp} /> </div> <div>{qty}</div> <div onClick={quantityMinus} className="qty-action"> <FontAwesomeIcon icon={faAngleDown} /> </div> </div> <div> <div onClick={quantityPlus} className="qty-action"> <FontAwesomeIcon icon={faAngleUp} /> </div> <div>{qty}</div> <div onClick={quantityMinus} className="qty-action"> <FontAwesomeIcon icon={faAngleDown} /> </div> </div> </div> ); };

Thanks for your efforts!感谢您的努力!

You'll want to create some form of a data structure to manage multiple quantities.您需要创建某种形式的数据结构来管理多个数量。 Since you're only using one qty variable right now, only one thing will ever change.由于您现在只使用一个qty变量,因此只有一件事会改变。

Let's say you have a simple CartItem interface to represent some shopping cart item that has quantity + a name or id of the item being purchased.假设您有一个简单的CartItem接口来表示一些购物车项目,该项目具有数量 + 所购买项目的名称或 ID。

(It could look something like this if you were using typescript.) (如果您使用的是 typescript,它可能看起来像这样。)

interface CartItem {
  name: string;
  quantity: number;
}

Then in React, you'd want to store a collection of these CartItem s in your state somewhere so you can track how many objects you need to render.然后在 React 中,您希望将这些CartItem的集合存储在您的 state 中的某个地方,以便您可以跟踪需要渲染的对象数量。 You could use an object, set, or just a simple array depending on your use case.根据您的用例,您可以使用 object、集合或只是一个简单的数组。 Let's use an array.让我们使用一个数组。

const [cartItems, setCartItems] = useState([{name: 'apple', quantity: 0}, {name: 'orange', quantity: 1}]); // initial data as an example

Next, your quantityUp/down functions need to be a little bit more generic so that they can receive a CartItem object as an argument and you can accordingly use that to figure out which cart item to update.接下来,您的 quantityUp/down 函数需要更通用一些,以便它们可以接收CartItem object 作为参数,您可以相应地使用它来确定要更新的购物车项目。

const changeQuantity = useCallback((item, quantityChange) => {
  // Find the item to update
  const oldCartItems = [...cartItems]; 
  const itemIndex = oldCartItems.findIndex((oldItem) => oldItem.name === item.name);

  // Change the quantity
  oldCartItems[itemIndex].quantity += quantityChange;

  // Put it back in state
  setCartItems(oldCartItems);
}, [cartItems]);

Finally, in your rendering/JSX, you don't need to keep writing the same JSX over and over again.最后,在您的渲染/JSX 中,您不需要一遍又一遍地编写相同的 JSX。 You can map through the cartItems state.您可以通过cartItems state。

return (
    <div>
      {cartItems.map((cartItem) => 
      <div>
        <div onClick={() => changeQuantity(cartItem, 1)} className="qty-action">
          <FontAwesomeIcon icon={faAngleUp} />
        </div>
        <div>{cartItem.quantity}</div>
        <div onClick={() => changeQuantity(cartItem, -1)} className="qty-action">
          <FontAwesomeIcon icon={faAngleDown} />
        </div>
      </div>
}
    </div>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM