javascript - 在反应中提交表单数据
问题描述
我有组件,在每个组件中,我都有一个表单,点击下一个我打开一个新表单(调用新组件),当它是最后一个时,我将其更改为提交。
我想要达到的目标
1:在每次下一步和返回单击时,我想使用 react-form-hook 验证我的表单
2:点击提交我应该能够看到所有的数据
3:点击后面的数据不应该从输入中丢失
问题
因此,对于验证,我使用react-hook-form,因为它使用起来非常简单,但是在这里我无法找到,因为我的按钮不在它们位于主要组件中的表单内,所以我将如何验证并提交表单提交点击所有数据。
我的代码
import React, { useState } from "react";
import Form1 from "./components/Form1";
import Form2 from "./components/Form2";
import Form3 from "./components/Form3";
function AddCompanyMain() {
const [currState, setCurrState] = useState(1);
const [allState, setAllstate] = useState(3);
const moveToPrevious = () => {
setCurrState(currState - 1);
};
const moveToNext = () => {
setCurrState(currState + 1);
};
return (
<div>
<div class="progress">
<div>{currState}</div>
</div>
{currState === 1 && <Form1 />}
{currState === 2 && <Form2 />}
{currState === 3 && <Form3 />}
{currState !== 1 && (
<button
className="btn btn-primary"
type="button"
onClick={moveToPrevious}
>
back
</button>
)}
{currState !== allState && (
<button className="btn btn-primary" type="button" onClick={moveToNext}>
next
</button>
)}
{currState === 3 && (
<button className="btn btn-primary" type="submit">
Submit
</button>
)}
</div>
);
}
export default AddCompanyMain;
我的完整代码代码沙箱
我只是在寻找一种好的方法,因为这里我不能在每个组件中都提交,因为我必须在顶部显示步骤。
解决方案
您可以通过执行以下操作来解决您的问题:
- 在 app.js 中,将表单存储在对象数组中,并根据步骤(currentFrom 状态)呈现它们。
- 调用
useForm
父组件(不在子 Form 组件中)并将register, errors, defaultValues
值作为道具传递给您的Form
组件。 - 在您的
Form
组件中,您需要register, etc
作为道具抓取 - 保持
defaultValues
状态并onClick
在下一个/上一个中更新它们。您需要getValues然后使用setValues设置状态(defaultValues)
还:
为了在下一个/上一个按钮单击时触发验证,您需要使用triggerValidation
为了在单击下一个/上一个时保留值,您需要使用defaultValues道具
在这里查看工作演示
代码片段
import React, { useState } from "react";
import Form1 from "./components/Form1";
import Form2 from "./components/Form2";
import Form3 from "./components/Form3";
import { useForm } from "react-hook-form";
function AddCompanyMain() {
const {
register,
triggerValidation,
errors,
setValue,
getValues
} = useForm();
const [defaultValues, setDefaultValues] = useState({});
const forms = [
{
fields: ["uname"],
component: (register, errors, defaultValues) => (
<Form1
register={register}
errors={errors}
defaultValues={defaultValues}
/>
)
},
{
fields: ["lname"],
component: (register, errors, defaultValues) => (
<Form2
register={register}
errors={errors}
defaultValues={defaultValues}
/>
)
},
{
fields: ["company"],
component: (register, errors, defaultValues) => (
<Form3
register={register}
errors={errors}
defaultValues={defaultValues}
/>
)
}
];
const [currentForm, setCurrentForm] = useState(0);
const moveToPrevious = () => {
setDefaultValues(prev => ({ ...prev, [currentForm]: getValues() }));
triggerValidation(forms[currentForm].fields).then(valid => {
if (valid) setCurrentForm(currentForm - 1);
});
};
const moveToNext = () => {
console.log(getValues());
setDefaultValues(prev => ({ ...prev, [currentForm]: getValues() }));
triggerValidation(forms[currentForm].fields).then(valid => {
if (valid) setCurrentForm(currentForm + 1);
});
};
const prevButton = currentForm !== 0;
const nextButton = currentForm !== forms.length - 1;
return (
<div>
<div class="progress">
<div>{currentForm}</div>
</div>
{forms[currentForm].component(
register,
errors,
defaultValues[currentForm]
)}
{prevButton && (
<button
className="btn btn-primary"
type="button"
onClick={moveToPrevious}
>
back
</button>
)}
{nextButton && (
<button className="btn btn-primary" type="button" onClick={moveToNext}>
next
</button>
)}
{currentForm === 3 && (
<button className="btn btn-primary" type="submit">
Submit
</button>
)}
</div>
);
}
export default AddCompanyMain;
注意 - 通常,在使用多步表单时,最好使用受控组件并维护父组件中的状态
推荐阅读
- android - 我需要从看起来像数组的 jsonobject 获取数据,但我只需要最后一个值,
- javascript - 我的 Heroku Discord 机器人离线 - discord.js
- javascript - 如何修复错误[WebSocket 已经处于 CLOSING 或 CLOSED 状态。]?
- python - 如何在 seaborn Pairplot 中设置 x 轴大小
- python - 使用暂停的 python 执行操作 matplotlib 绘图,复制 matlab 暂停行为
- javascript - 我怎样才能使分页工作?
- java - 查找第二次出现的子字符串的 Java 方法中的“错误:找不到符号”
- django - PyCharm调试Django REST框架api:断点不停
- bash - 如何在错误时终止 kubectl port-forward?
- javascript - 使用 fs.rmdir 递归删除后,空文件夹仍然存在