-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: useWatch return latest form value instead of the default value #2739
Comments
Thanks for the feature request, it's been updated to the board. |
hey @cYee992 think twice, i think you can achieve this problem by the following: useWatch({
name: 'test',
defaultValue: getValues().test
}) |
Thanks for the that. I just saw your comment in the midst of submitting a pull request... missed your comment. In this line react-hook-form/src/useWatch.ts Line 62 in a5197c1
Changed to this In useWatch
|
Yea it's possible. what's the usage like? cause it's pretty easier to provide a defaultValue at useWatch as well, save that nested logic inside useWatch
|
Yup, it is really easy to provide the default value, just that it feels weird whenever you unmount and remount the data display component, value goes back to initial. Could be some gotcha moment. The usage will be whenever you are unmounting and remounting the component, the value will be the latest fieldRef value:
|
valid case @cYee992 thanks for the detail. This may be a breaking change to existing users with |
I found that watch API actually has the precidence where fieldRef value > default value. react-hook-form/src/useForm.ts Line 731 in 437eb09
|
also, be mindful that be default, react hook form unregister input after they get unmounted, so even this feature going through it will only applicable for shouldUnregsiter: false. for shouldUnregister: true by default: https://codesandbox.io/s/hungry-waterfall-kyp4s |
Yup. Correct. The field will be erased if the main input got erased. but as long as the field input still there, eg: modal and re-mounting the data display component still able to get back the fieldRef value. |
Yes, personally I am lean towards not include that extra logic into |
Alright. Thanks for your time. |
Sorry, i am moving it back to request feature board. |
Thank you! |
This is getting fixed in the next release. |
I can confirm this issue is still present in the latest version. @bluebill1049 is it a regression? |
Is your feature request related to a problem? Please describe.
If the component with useWatch is unmounted and remount, the form value displayed default value instead of latest form value. Example use case: a long form, user can toggle to peak on the form summary and close it, but the value will be the initial default values.
Describe the solution you'd like
Returning latest form value whenever useWatch is mounted instead of default value
Issue related to this:
#2738
The text was updated successfully, but these errors were encountered: