admin管理员组文章数量:1418302
I'm working on implementation of a multi step form with react-hook-form
and my problem is that input fields do not get reinitialized with the form data when I return to the previous page.
I'm using <FormProvider />
ponent from react-hook-form
to inject the form data into the pages and my input
ponents are registered with register
method from useFormContext()
hook
const CreateAccount = () => {
const [currentStep, setCurrentStep] = useState(0);
const methods = useForm<FormData>({
mode: "onChange",
});
const onSubmit = (data) => console.log(data);
const handleNextStep = () => {
if (currentStep >= 5) return;
setCurrentStep(currentStep + 1);
};
const handlePreviousStep = () => {
if (currentStep <= 0) return;
setCurrentStep(currentStep - 1);
};
const renderContent = () => ({
[RegistrationSteps.UsernameEmail]: <UsernameEmail handleNextStep={handleNextStep} handlePreviousStep={handlePreviousStep} />,
[RegistrationSteps.Password]: <CreatePassword handleNextStep={handleNextStep} handlePreviousStep={handlePreviousStep} />,
});
return (
<Container maxWidth="sm">
<FormProvider {...methods}>
<form onSubmit={methods.handleSubmit(onSubmit)}>
{renderContent()[currentStep]}
</form>
</FormProvider>
</Container>
);
};
export default CreateAccount;
Here is what the input fields look like
const {
register
} = useFormContext();
<TextField
label="Email"
{...register("email")}
/>
Even though the form still holds the data in its state, it does not populate into corresponding fields when I switch back and forth between the form pages.
I'm working on implementation of a multi step form with react-hook-form
and my problem is that input fields do not get reinitialized with the form data when I return to the previous page.
I'm using <FormProvider />
ponent from react-hook-form
to inject the form data into the pages and my input
ponents are registered with register
method from useFormContext()
hook
const CreateAccount = () => {
const [currentStep, setCurrentStep] = useState(0);
const methods = useForm<FormData>({
mode: "onChange",
});
const onSubmit = (data) => console.log(data);
const handleNextStep = () => {
if (currentStep >= 5) return;
setCurrentStep(currentStep + 1);
};
const handlePreviousStep = () => {
if (currentStep <= 0) return;
setCurrentStep(currentStep - 1);
};
const renderContent = () => ({
[RegistrationSteps.UsernameEmail]: <UsernameEmail handleNextStep={handleNextStep} handlePreviousStep={handlePreviousStep} />,
[RegistrationSteps.Password]: <CreatePassword handleNextStep={handleNextStep} handlePreviousStep={handlePreviousStep} />,
});
return (
<Container maxWidth="sm">
<FormProvider {...methods}>
<form onSubmit={methods.handleSubmit(onSubmit)}>
{renderContent()[currentStep]}
</form>
</FormProvider>
</Container>
);
};
export default CreateAccount;
Here is what the input fields look like
const {
register
} = useFormContext();
<TextField
label="Email"
{...register("email")}
/>
Even though the form still holds the data in its state, it does not populate into corresponding fields when I switch back and forth between the form pages.
Share Improve this question asked Jul 21, 2021 at 13:58 Oleksandr FominOleksandr Fomin 2,3868 gold badges30 silver badges51 bronze badges1 Answer
Reset to default 4Instead of a single form at a global level, I remend creating each ponent in your step as a form with its own instance of useForm()
and wrapping steps in a state provider to store data across different steps. That way, you can assign values to the step forms from the respective state using defaultValues
option of useForm
on initialization.
You can check out this for the basic architecture that I'm trying to explain.
defaultValues in useForm
本文标签: javascriptreacthookform multi step form issueStack Overflow
版权声明:本文标题:javascript - react-hook-form multi step form issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745290341a2651754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论