-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Fix FormDataConsumer's values not being up-to-date on mount #8340
Conversation
Let's wait for @fzaninotto 's review before merging this one |
@@ -43,22 +43,25 @@ import warning from '../util/warning'; | |||
* ); | |||
*/ | |||
const FormDataConsumer = (props: ConnectedProps) => { | |||
const formData = useWatch(); | |||
const { getValues } = useFormContext(); | |||
let formData = useWatch(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useWatch
docs:
The initial return value from useWatch will always return what's inside of defaultValue or defaultValues from useForm.
Initial discussion:
react-hook-form/react-hook-form#3758
Is not a bug, is a feature of react-hook-form.
So, this fix seems reasonable
I understand that useWatch returns the form defaultValues, which is an empty record by default. But when the Form parent passes a record, it resets the form values, and useWatch should return the right values in this case. I saw this in the react-hook-form docs: Isn't that the problem? |
@fzaninotto I think I have a clear proof that your intuition is correct. I think it makes for a very dirty gotcha, that we need to either document or, preferably, fix. However, I can think of no better solution than what @WiXSL suggests. FTR I have tried to manually call |
We need help from @bluebill1049 on this one. I suggest you go to react-hook-form's Discord server and ask for help. |
I've made a codesandbox that roughly reproduces the situation by using react-hook-form only: https://codesandbox.io/s/eager-hill-o4g71q?file=/src/App.tsx And I've asked about the problem in react-hook-form's discord |
I've improved the tests to guarantee the component is reactive. |
Fixes #7603