发布请求不适用于提取和反应
将在脉轮UI模式中输入的数据发送帖子请求
function Create() {
const { isOpen, onOpen, onClose } = useDisclosure();
const [sku, setSku] = React.useState("");
const [name, setName] = React.useState("");
const [unit, setUnit] = React.useState("");
const handleChangesku = (event) => setSku(event.target.value);
const handleChangeName = (event) => setName(event.target.value);
const handleChangeUnit = (event) => setUnit(event.target.value);
function handleClick() {
fetch("/products", {
method: "POST",
body: JSON.stringify({
sku: sku,
name: name,
primary_unit: unit
})
}).then((res) => res.json());
// Update the state with the received response
}
return (
<>
<Button onClick={onOpen}>Create Product</Button>
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Create Product</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Text mb="8px">SKU</Text>
<Input
value={sku}
onChange={handleChangesku}
placeholder="SKU"
size="sm"
/>
<Text mb="8px">Name</Text>
<Input
value={name}
onChange={handleChangeName}
placeholder="Name"
size="sm"
/>
<Text mb="8px">Unit</Text>
<Input
value={unit}
onChange={handleChangeUnit}
placeholder="Unit"
size="sm"
/>
</ModalBody>
<ModalFooter>
<Button colorScheme="blue" mr={3} onClick={onClose}>
Cancel
</Button>
<Button colorScheme="blue" mr={3} onClick={handleClick}>
Create
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
}
export default Create;
以下是我的代码,当我在模态中单击创建时,在我运行此代码时, 。检查检查面板的网络部分时,我会遇到以下错误:
0: {field: "sku", message: "This field is required"}
1: {field: "name", message: "This field is required"}
2: {field: "primary_unit", message: "This field is required"}
如何解决此问题?我试图使用Hook Usecallback,而不仅仅是普通的HandleClick来更改USESTATE变量的值,但它不起作用。我是否一定需要表格,以便我可以添加onsubmit?
Below is my code to send a post request with the data entered in a Chakra UI modal
function Create() {
const { isOpen, onOpen, onClose } = useDisclosure();
const [sku, setSku] = React.useState("");
const [name, setName] = React.useState("");
const [unit, setUnit] = React.useState("");
const handleChangesku = (event) => setSku(event.target.value);
const handleChangeName = (event) => setName(event.target.value);
const handleChangeUnit = (event) => setUnit(event.target.value);
function handleClick() {
fetch("/products", {
method: "POST",
body: JSON.stringify({
sku: sku,
name: name,
primary_unit: unit
})
}).then((res) => res.json());
// Update the state with the received response
}
return (
<>
<Button onClick={onOpen}>Create Product</Button>
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Create Product</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Text mb="8px">SKU</Text>
<Input
value={sku}
onChange={handleChangesku}
placeholder="SKU"
size="sm"
/>
<Text mb="8px">Name</Text>
<Input
value={name}
onChange={handleChangeName}
placeholder="Name"
size="sm"
/>
<Text mb="8px">Unit</Text>
<Input
value={unit}
onChange={handleChangeUnit}
placeholder="Unit"
size="sm"
/>
</ModalBody>
<ModalFooter>
<Button colorScheme="blue" mr={3} onClick={onClose}>
Cancel
</Button>
<Button colorScheme="blue" mr={3} onClick={handleClick}>
Create
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
}
export default Create;
When I run this code nothing happens when I click on create in the modal. I get the following error when checking the network section of inspection panel:
0: {field: "sku", message: "This field is required"}
1: {field: "name", message: "This field is required"}
2: {field: "primary_unit", message: "This field is required"}
How can I fix this? I tried to use the hook UseCallback instead of just normal handleclick to change the values of the useState variables but it did not work. Do I necessarily need a form so that I can add an OnSubmit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论