CLOSED: RFC useForm defaultValuesStrategy: shallow & deep #5087
Replies: 8 comments 19 replies
-
cc @kotarella1110 @jorisre @leapful (team) |
Beta Was this translation helpful? Give feedback.
-
Nice! If this is a feature that others would like I totally see it as a great addition to the library 👍 For more complex form models this may incur a noticeable perf cost - so it would be interesting to do some benchmarking.
Perhaps using a "lazy" deep merge/clone is possible? Eg. a deep clone that only shallowly copies the objects that are part of the path to the value being edited? They should be cheaper than a full deep merge. See this SO post for context. |
Beta Was this translation helpful? Give feedback.
-
I have one comment. We introduced
If we have the option above, do we need this feature anymore? It seems like Additionally what would happen if the user adds I think the cleanest solution is to remove the shallow merging modification from |
Beta Was this translation helpful? Give feedback.
-
we may no longer this config. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I just like to say a very big thank you, for the latest update 7.6.0 that has auto register of values (simulating auto deep merge)! I had tried to raise this issue early on the big V7 discussion thread, but was not making any headway, and I am glad now there is enough community support to get this implemented. Due to our very dynamic & very nested forms, it was very difficult for us to register every nested value we want to submit in the form. To be honest, we had already started migrating away from RHF to React-Final-Form due to this (it was that big a dealbreaker to us), but thankfully I wrote this as a separate own API layer that allowed me to switch back from RFF to RHF, and things started working beautifully with the latest update. We would very much have preferred to remain in RHF and this allowed us to do so. Thanks once again for all the great and speedy work put in, we really appreciate it. 👍👍 |
Beta Was this translation helpful? Give feedback.
-
Does this get disabled if we set |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Context
React Hook Form embraces uncontrolled form and focuses on performance and user experience. This is one of the core design principles which we have followed. However, there are cases where developer experience gets sacrificed due to
defaultValues
is not the local state and often users expectdefaultValues
to be part of the form values, and hence we have introduced shallow merge to encounter that without sacrifice performance.This approach solves most of the use cases but leaves deeply nested
defaultValues
get overwrite due to shallow merge.For example the following use case:
Proposal
We are considering exposing a new config at
useForm
which allows users to choose the merge strategy.Here is the proposed syntax
Considerations
shouldUnregister: false
which is by default.API coverage
handleSubmit
getValues
watch
/useWatch
Props
defaultValues
behaviordefaultValues
without getting overwriteCons
deep
will have a relative performance impact for large and complex formLimitation
Related issues/feature requests
#5003
#5056
Draft PR
#5069
Beta Was this translation helpful? Give feedback.
All reactions