π fix #12772 reset form useWatch to utilize ref for defaultValue and β¦ #12780
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π fix #12772 reset form
Problem:
Previously, the
useWatchhook could sometimes fail to immediately reflect the form values after aresetoperation, particularly whendefaultValuewas provided and used in conjunction withController. This could lead to a scenario where a user had to trigger the reset action twice for theuseWatchvalue to update correctly to the default valueThe likely cause was related to how
defaultValuewas managed and included in the dependency array ofuseDeepEqualEffect. When the form was reset, the internal handling of default values could potentially causeuseWatch's internal subscription to briefly become out of sync with the form's actual state, as theuseDeepEqualEffectmight trigger a re-subscription based on a perceived change in thedefaultValueobject reference or content during the reset process. This timing mismatch meant the hook might not pick up the newly reset value immediately.Solution:
This change modifies how
defaultValueis handled within theuseWatchhook. Instead of includingdefaultValuein theuseEffectdependency array (previously viauseDeepEqualEffect),defaultValueis now stored in auseRef.By storing
defaultValuein auseRef, its value can be accessed within the effect callback without being part of the dependency array.Impact:
This ensures that the subscription established by
useWatchis not unnecessarily torn down and re-established solely due to potential changes or re-creations of thedefaultValueobject during the form reset process. The subscription remains stable, allowinguseWatchto reliably pick up and reflect the form's state, including the correctly applied default values, immediately afterresetis called. This resolves the issue where a second reset was sometimes required.