如何增加和减少ReactJ的数量?

发布于 2025-01-26 19:49:08 字数 1178 浏览 2 评论 0原文

我有一个按钮和一个输入表格。单击按钮时,我的产品数量减少1,并在提交表单时增加数量。数量随形式值增加。如何在ReactJ中完成此问题?

import React, { useEffect, useState } from "react";

const InventoryDetails = () => {
  const [inventory, setInventory] = useState({});

  useEffect(() => {
    const url = `inventory.json`;

    fetch(url)
      .then((res) => res.json())
      .then((data) => setInventory(data));
  }, []);

  const handleDelivered = () => {
    // can't understand what can I do here.
  };
  const restockItem = () => {};

  return (
    <div>
      <div>
        <h6> Quantity: {inventory.quantity} </h6>

        <button onClick={handleDelivered}>Delivered</button>
      </div>

      <div>
        <h3>Restock the items</h3>
        <form onSubmit={restockItem}>
          <input
            type="number"
            name="number"
            id=""
            placeholder="Enter quantity"
            required
          />

          <input className="" type="submit" value="Restock" />
        </form>
      </div>
    </div>
  );
};

I have a button and an input form. When clicking the button, my product quantity decreases by 1 and increases quantity when submitting the form. Quantity increases with the form value. How can I complete this problem in Reactjs?

import React, { useEffect, useState } from "react";

const InventoryDetails = () => {
  const [inventory, setInventory] = useState({});

  useEffect(() => {
    const url = `inventory.json`;

    fetch(url)
      .then((res) => res.json())
      .then((data) => setInventory(data));
  }, []);

  const handleDelivered = () => {
    // can't understand what can I do here.
  };
  const restockItem = () => {};

  return (
    <div>
      <div>
        <h6> Quantity: {inventory.quantity} </h6>

        <button onClick={handleDelivered}>Delivered</button>
      </div>

      <div>
        <h3>Restock the items</h3>
        <form onSubmit={restockItem}>
          <input
            type="number"
            name="number"
            id=""
            placeholder="Enter quantity"
            required
          />

          <input className="" type="submit" value="Restock" />
        </form>
      </div>
    </div>
  );
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2025-02-02 19:49:08

在这里,我编写了两个函数andledeledretockiTem。将retockitem函数函数到您的按钮,然后尝试:

handleDelivered = () => {
    setInventory({
        ...inventory,
        quantity: inventory.quantity - 1,
    });
};

restockItem = (event) => {
    event.preventDefault();

    setInventory({
        ...inventory,
        quantity: inventory.quantity + parseInt(event.target[0].value),
    });
};

您必须在进行更改时设置库存状态,...库存正在破坏<<代码>库存对象将所有字段获取内部并仅更改数量字段,destreddefault只是我的习惯数量字段。

Here I wrote two functions handleDelivered and restockItem. Put restockItem function to your button and try them:

handleDelivered = () => {
    setInventory({
        ...inventory,
        quantity: inventory.quantity - 1,
    });
};

restockItem = (event) => {
    event.preventDefault();

    setInventory({
        ...inventory,
        quantity: inventory.quantity + parseInt(event.target[0].value),
    });
};

You have to set the state of the inventory when you're going to make changes, ...inventory is destructing the inventory object to get all the fields inside and change only quantity field, preventDefault is just my habit to prevent submitting, don't forget to type check the quantity field.

醉态萌生 2025-02-02 19:49:08

您可以使用这些技巧来解决此问题 -

  1. 当您从股票中添加或删除数量时,还可以使用“ put”方法更新数据库,并设置对您对产品的使用效果的依赖。因此,当产品数据在数据库中更新时,由于依赖关系,它也会在UI中更新。

You can use these tricks to solve this problem -

  1. When you add or remove quantity from your stocks you can also update the database also using a 'PUT' method and set a dependency on your use effect over product. So when the product data updated in the database its also updated in the UI because of the dependency.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文