Replies: 2 comments 2 replies
-
Hi @wdfinch thanks for the suggestion. I think if you dig around in the example folder, someone has submitted an example. The rational on the design with react hook form is more aligned with native form behavior which includes its validation when an input gets unmounted and value should be removed (according to the current design) However, we do understand the concern and inconvenient to change most of us (React developer's mindset), hence I have introduced Those are my personal take on the matter, as more developers having issues around inputs get unmounted and values get removed, (which can be solved by toggle the CSS as well) we are removing that config together in v7, and users will have to unregister manually. you can read more on v7 RFC. |
Beta Was this translation helpful? Give feedback.
-
Hi, @bluebill1049 thanks for the quick reply. I read through the v7 RFC and from my understanding it sounds like values will be persisted by default in v7 even after an unmount. If that's the case, that sounds like a good solution to this problem. I understand you're reasoning and design goals. It's more true to the original design goals of the form tag and that makes sense. For me personally, I view a form as containing a set of inputs. Whether you create many small separate forms or one large form really depends on how disjoint/separated the inputs are. I think in examples like the one you have for the wizard forms where the forms are on different routes, it makes sense to separate the inputs into separate forms as they are very disjoint. However, this is somewhat of an extreme example (merging form data across multiple pages/routes). In most cases, a form will be on a single page and contain a small set of connected inputs. Whether they are all visible on the screen or not is merely a UI detail. For example, if want to hide some inputs because they are taking up too much space, I shouldn't need a separate While CSS can be used, I don't think it's very clean nor react like. Additionally, it's can negatively impact validation. For example, I might want to allow the user to proceed to the next step in a wizard form when the mounted input is valid, but using This (the use of Lastly, something to consider is the ongoing increase in the complexity of websites. When many of the HTML tags and native behaviors were defined, the web was much simpler. However, as time has gone on, websites have grown exponentially in complexity. With that many of the native behaviors just don't cover more advanced use cases (which are becoming more common as time goes on). I like how the library stays true to the native behavior and understand the downside of adding too many features/becoming too broad. However, as time goes on more and more users will likely need to cover advanced use cases far beyond the original behavior of the form tag. Obviously, you and the community must decide what's best for the library but just something to think about from an outside perspective and someone who works on large/complex React apps. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It is common for large forms to be spread over multiple components (wizard forms) and/or be expandable (a toggle to hide/reveal part of a large form).
Right now it appears the library will only store form values that are mounted and will remove form values when they are unmounted. This leaves it up to the user to store these values (via redux, useState, context, etc.) and inject them back into the form if the component is mounted again.
This becomes tedious over many forms and seems like an unnecessary burden to place on the user. However, I do understand that there are cases where form values should be removed on unmount. For example, a user clicks a checkbox to reveal some additional options and then decides to unclick the checkbox to remove those options. We can assume those options are associated with the checkbox and are irrelevant if the box is not checked.
To meet both use cases, I propose adding a new prop to
register
and<Controller/>
calledpersistValue
or something similar. If this prop is used, then the value of the field will be persisted even after the component is unmounted. Obviously, it would not be persisted when theuseForm
component is unmounted. This would make it much easier for the user to keep track of values in conditionally rendered fields, while still keeping the default behavior and option of removing values on unmount.What do you think?
Beta Was this translation helpful? Give feedback.
All reactions