如何增加和减少ReactJ的数量?
我有一个按钮和一个输入表格。单击按钮时,我的产品数量减少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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里,我编写了两个函数
andledeled
和retockiTem
。将retockitem
函数函数到您的按钮,然后尝试:您必须在进行更改时设置库存状态,
...库存
正在破坏<<代码>库存对象将所有字段获取内部并仅更改数量
字段,destreddefault
只是我的习惯数量字段。Here I wrote two functions
handleDelivered
andrestockItem
. PutrestockItem
function to your button and try them:You have to set the state of the inventory when you're going to make changes,
...inventory
is destructing theinventory
object to get all the fields inside and change onlyquantity
field,preventDefault
is just my habit to prevent submitting, don't forget to type check the quantity field.您可以使用这些技巧来解决此问题 -
You can use these tricks to solve this problem -