我有一些组件,每个组件中都有一个表单,然后单击一次,我打开一个新表单(称为新组件),最后一次,我将其更改为提交。
我要实现的目标
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;
我的完整代码Code sandbox
我只是在寻找一种好的方法,因为在这里我不能在每个组件中放置提交,因为我必须在顶部显示步骤。
答案 0 :(得分:1)
您可以lift the value state of each component up to the parent,然后将onChange道具传递给每个对象,以更新每个孩子的状态。我不确定这将如何与react-hooks-form一起使用,但是通常这就是您要同步子组件状态的方式。
import React from "react";
import "./styles.css";
const FormOne = ({ value, onChange }) => (
<div>
Form 1
<input type='text' value={value} onChange={e => onChange(e.target.value)} />
</div>
)
const FormTwo = ({ value, onChange }) => (
<div>
Form 2
<input type='text' value={value} onChange={e => onChange(e.target.value)} />
</div>
)
const FormThree = ({ value, onChange }) => (
<div>
Form 3
<input type='text' value={value} onChange={e => onChange(e.target.value)} />
</div>
)
export default function App() {
const [formOne, setFormOne] = React.useState('')
const [formTwo, setFormTwo] = React.useState('')
const [formThree, setFormThree] = React.useState('')
const [index, setIndex] = React.useState(0)
const moveUp = (e) => {
e.preventDefault()
if (index !== 2) {
setIndex(index => index += 1)
}
}
const moveDown = (e) => {
e.preventDefault()
if (index !== 0) {
setIndex(index => index -= 1)
}
}
const handleSubmit = (e) => {
e.preventDefault()
console.log(formOne, formTwo, formThree)
}
const form = index === 0
? <FormOne value={formOne} onChange={setFormOne} />
: index === 1
? <FormTwo value={formTwo} onChange={setFormTwo} />
: <FormThree value={formThree} onChange={setFormThree} />
return (
<div className="App">
<form onSubmit={handleSubmit}>
{form}
{index !== 0 && <button onClick={moveDown}>Back</button>}
{index !== 2
? <button onClick={moveUp}>Next</button>
: <button type='submit'>Submit</button>
}
</form>
</div>
);
}
答案 1 :(得分:1)
您可以通过执行以下操作来解决您的问题:
useForm
,并将register, errors, defaultValues
的值作为props传递给Form
组件。Form
组件中,您需要抓住register, etc
作为道具defaultValues
保持在一个状态,并在下一个/上一个的onClick
中进行更新。您需要先getValues,然后使用setValues来设置状态(默认值)也:
要在单击上一个/下一个按钮时触发验证,您需要使用triggerValidation
要在单击下一个/上一个时保留值,您需要使用defaultValues prop
代码段
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;
注意-通常,在使用多步骤表单时,最好使用受控组件并保持父组件中的状态