issue: useWatch does not return default value when Component is dynamically rendered #10201
Replies: 5 comments 1 reply
-
My use case is that I have a complex form where the relations among some field components are fetched from the database. These field components are rendered only after they have finished fetching. Otherwise, they will display a loading status. |
Beta Was this translation helpful? Give feedback.
-
This is related to the subscription execution order. https://react-hook-form.com/api/usewatch/
|
Beta Was this translation helpful? Give feedback.
-
Hi Bill, thank you for your reply. However, I don't understand why the problem is related to the execution order. The early return ensures that form inputs are only rendered once the watcherSetting state is set. In this case, the execution order of these two forms should be the same. However, based on my observations, '_state.mount' is set to true after the first render. This causes useWatch in the second form to look up values only in '_formValues' instead of looking up values in its defaultValue as well. This seems inconsistent with the documentation:
I would expect the return value fallback to 'defaulValue' in useWatch when global 'defaultValues' in useForm is not provided, and I believe this was possible before 7.41.0. |
Beta Was this translation helpful? Give feedback.
-
After the components component, we will return |
Beta Was this translation helpful? Give feedback.
-
when https://codesandbox.io/p/sandbox/rfh-usewatch-multiple-fields-z2h2rt?file=%2Fsrc%2FApp.js |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Version Number
^7.41.0
Codesandbox/Expo snack
https://codesandbox.io/s/react-hook-form-v7-usewatch-forked-evult8?file=/src/index.js
Steps to reproduce
Expected behaviour
the second useWatcher returns the same default value as the first one.
What browsers are you seeing the problem on?
No response
Relevant log output
No response
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions