[英]How to add form values on submit to a table using React Hooks
I'm building a project management app, and I'm a bit new to Hooks, so please bear with me.我正在构建一个项目管理应用程序,我对 Hooks 有点陌生,所以请耐心等待。
Link to the app , link to the code .链接到应用程序,链接到代码。
The user should have the ability to add a new project.用户应该能够添加新项目。 They can add a name, description and date, which would then be automatically added to the table.他们可以添加名称、描述和日期,然后这些信息会自动添加到表格中。
Currently, I'm unable to get it working.目前,我无法让它工作。 A user is unable to add a new project.用户无法添加新项目。
I can type in a new date in the Date field - but anytime I try to type in the Name or Description field, the values don't show and I get console errors thrown:我可以在“日期”字段中输入新日期 - 但每当我尝试在“名称”或“描述”字段中输入时,这些值都不会显示,并且会引发控制台错误:
"Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the method isDefaultPrevented
on a released/nullified synthetic event. This is a no-op function. If you must keep the original synthetic event around, use event.persist()." “警告:出于性能原因重用此合成事件。如果您看到这一点,您正在访问方法isDefaultPrevented
在已发布/无效的合成事件上。这是一个无操作功能。如果您必须保留原始合成事件周围,使用 event.persist()。”
The main issues that I'm trying to fix:我试图解决的主要问题:
In my code, mainCrud.js houses the initial data as well as the Add Project functionality.在我的代码中,mainCrud.js 包含初始数据以及添加项目功能。 The data's passed down into the actual CrudAdd component, which houses the "Add Project" form:数据向下传递到实际的 CrudAdd 组件中,该组件包含“添加项目”表单:
import React, { useState } from "react";
import CrudIntro from "../crud/crudIntro/crudIntro";
import CrudAdd from "../crud/crudAdd/crudAdd";
import CrudTable from "../crud/crudTable/crudTable";
const MainCrud = props => {
// Project Data
const projectData = [
{
id: 1,
name: "Skid Steer Loaders",
description:
"To advertise the skid steer loaders at 0% financing for 60 months.",
date: "February 1, 2022"
},
{
id: 2,
name: "Work Gloves",
description: "To advertise the work gloves at $15.",
date: "February 15, 2022"
},
{
id: 3,
name: "Telehandlers",
description: "To advertise telehandlers at 0% financing for 24 months.",
date: "March 15, 2022"
}
];
const [projects, setProject] = useState(projectData);
// Add Project
const addProject = project => {
project.id = projectData.length + 1;
setProject([...projects, project]);
};
return (
<div>
<section id="add">
<CrudIntro title={props.title} subTitle={props.subTitle} />
<CrudAdd addProject={addProject} />
</section>
<section id="main">
<CrudTable projectData={projects} />
</section>
</div>
);
};
export default MainCrud;
In CrudAdd.js, I'm setting the initial state of the form, updating the state of the form and adding the form submit:在 CrudAdd.js 中,我正在设置表单的初始状态,更新表单的状态并添加表单提交:
import React, { useState } from "react";
import "../crudAdd/crud-add.scss";
import "../../button.scss";
const CrudAdd = props => {
// Set the initial state of the form
const initialFormState = {
id: null,
name: "",
description: "",
date: ""
};
const [project, setProject] = useState(initialFormState);
// Update state within the form
const handleInputChange = e => {
const { name, value } = e.target;
setProject({ ...project, [name]: value });
console.log(e);
};
return (
<div>
<form
onSubmit={e => {
e.preventDefault();
if (!project.name || !project.username) return;
props.addProject(project);
setProject(initialFormState);
}}
>
<input
type="name"
placeholder="Name..."
id="name"
value={project.name}
onChange={handleInputChange}
/>
<input
type="description"
placeholder="Description..."
id="description"
value=""
/>
<input type="name" placeholder="Date..." id="date" />
</form>
<button className="btn btn-primary">Add Project</button>
</div>
);
};
export default CrudAdd;
Any idea?任何的想法? Thanks in advance.提前致谢。
I got what's the problem in your code.我知道你的代码有什么问题。 You have not add name
property in your input fields您尚未在输入字段中添加name
属性
<input name="name" type="name" placeholder="Name..." id="name" value={project.name} onChange={handleInputChange} /> <input name="description" type="description" placeholder="Description..." id="description" value={project.description} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.