How to implement RHF for multiple duplicate forms on the same page #10793
Unanswered
wkd-kapsule
asked this question in
Q&A
Replies: 1 comment 2 replies
-
Having the same issue. Same form rendered twice, elements composed differently for two viewports. Leads to duplicated ids on the page. @bluebill1049 I noticed you closed this August 20th 2023. Is there a solution? #10808 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
My situation is the following:
The users of my SaaS can create a product which can have multiple variants. On the creation page, the variants are displayed like in tabs. Only one is visible at a time and you can navigate through them via a corresponding tab's link. When the user creates a new variant, it is stored in an array with the others and the parent component only renders one a time by going through the array and selecting the one corresponding to the current view (the current view is kept as a state that changes when the user clicks on the corresponding tab's link).
My structure (very simplified) looks like this :
The important thing is that I have a unique button to save the currently displayed form. Inside each
<VariantPanel/>
, I useuseFormContext()
to register the inputs and get the errors to display. As you can guess it, everything is working fine until I have multiple variants: they all share the same data.For example, if errors are displayed on one variant (form), all variants, even a brand new one, would also have the errors displayed. The same goes for the data registered for each input.
I don't know if it's because they are duplicates and so there inputs having the same name is causing the issue. Or if it's because they share the same
<FormProvider/>
, thus the sameregister()
,handleSubmit()
, ...What would be the correct approach to solve my issue? I was thinking about putting a unique FormProvider inside each
<VariantPanel/>
. But then, how could the submit button in the parent component access the correcthandleSubmit()
to execute?Any help would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions