Skip to content
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

Merged
merged 9 commits into from
Nov 17, 2022

Conversation

WiXSL
Copy link
Contributor

@WiXSL WiXSL commented Nov 1, 2022

Fixes #7603

@slax57
Copy link
Contributor

slax57 commented Nov 2, 2022

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();
Copy link
Contributor Author

@WiXSL WiXSL Nov 8, 2022

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

@fzaninotto
Copy link
Member

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:

image

Isn't that the problem?

@slax57
Copy link
Contributor

slax57 commented Nov 10, 2022

@fzaninotto I think I have a clear proof that your intuition is correct.
If you invert the two components <FormDataConsumer> and <BooleanInput> in @andrico1234 's PostCreate, then you no longer have the issue.

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.
Hence I advocate for accepting and merging this PR as is.

FTR I have tried to manually call form.watch() in useAugmentedForm, but it doesn't work.

@fzaninotto
Copy link
Member

fzaninotto commented Nov 10, 2022

We need help from @bluebill1049 on this one. I suggest you go to react-hook-form's Discord server and ask for help.

@WiXSL
Copy link
Contributor Author

WiXSL commented Nov 14, 2022

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

@WiXSL
Copy link
Contributor Author

WiXSL commented Nov 15, 2022

I've improved the tests to guarantee the component is reactive.
So far, no news from discord

@fzaninotto fzaninotto merged commit a5a4b5b into master Nov 17, 2022
@fzaninotto fzaninotto deleted the fix-fdc-onmount branch November 17, 2022 14:37
@fzaninotto fzaninotto added this to the 4.5.2 milestone Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Input level default values aren't applied to the formState on mount
4 participants