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

issue: Usage of useController hook, change isDirty state for form to true. #10143

Closed
1 task done
sshmyg opened this issue Mar 20, 2023 · 12 comments
Closed
1 task done

Comments

@sshmyg
Copy link

sshmyg commented Mar 20, 2023

Version Number

7.43.7

Codesandbox/Expo snack

https://codesandbox.io/s/react-hook-form-get-started-forked-d470cp?file=/src/index.js:239-246

Steps to reproduce

  1. Open https://codesandbox.io/s/react-hook-form-get-started-forked-d470cp?file=/src/index.js:239-246
  2. See console log with 2 messages, first with isDirty as false and second - true.

Expected behaviour

Initial render shouldn't change isDirty for form.

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bluebill1049
Copy link
Member

expected behaviour as your formValues is no longer match your defaultValues

@sshmyg
Copy link
Author

sshmyg commented Mar 20, 2023

@bluebill1049 Why dirtyFields param is empty then?

@sshmyg
Copy link
Author

sshmyg commented Mar 20, 2023

@bluebill1049 And why formValues not match with defaultValues on first load?

@bluebill1049 bluebill1049 reopened this Mar 20, 2023
@bluebill1049
Copy link
Member

bluebill1049 commented Mar 20, 2023

Please don't tag me twice, I have loads of notifications try to keep up to date.

@bluebill1049 And why formValues not match with defaultValues on first load?

Screenshot 2023-03-20 at 11 17 29 pm

because input gets register at render.

@bluebill1049 Why dirtyFields param is empty then?

This something we can improve.

@bluebill1049 bluebill1049 added the enhancement New feature or request label Mar 20, 2023
@sshmyg
Copy link
Author

sshmyg commented Mar 20, 2023

Thnks, sorry for tags :)

@sshmyg
Copy link
Author

sshmyg commented Mar 20, 2023

Don't you think register and useController should have same behaviour? I mean if register doesn't change isDirty, useController shouldn't either? Or both should change.

@bluebill1049
Copy link
Member

useController called before render.

@sshmyg
Copy link
Author

sshmyg commented Mar 20, 2023

So you think different behaviour is ok?
Maybe this difference need to be mentioned in doc or something. It's not obvious.

@bluebill1049
Copy link
Member

It would be best if you always considered providing the entire default state for your entire form. In fact, this has been listed in the formState isDirty section: https://react-hook-form.com/api/useform/formstate/

@sshmyg
Copy link
Author

sshmyg commented Mar 20, 2023

I think react-hook-forms should not convert undefined to empty string for input values. React has warning for this, but with this implicit conversion, developer has no idea about this, and gets such unpredictable situation.
image

@bluebill1049
Copy link
Member

That's unless we remove all the inline defaultValue, defaultCheck support.

@bluebill1049 bluebill1049 added design limitation and removed enhancement New feature or request labels Mar 21, 2023
@bluebill1049
Copy link
Member

bluebill1049 commented Mar 21, 2023

Spend some time with this issue, I am going to keep it as it is for now as mark it as a design limitation. Yes, in hook form defeaultValues will be the source of truth to mark dirty fields and dirty values. I will include this information in the doc, to info the developer to include defaultValues to have better dirty tracking and consistent output. Probably going to dismiss this PR as well #10152 as it won't change the nature of importance of defaultVlaues for react hook form.

react-hook-form/documentation@8f92ebb

@bluebill1049 bluebill1049 added improve documentation documentation update required and removed improve documentation documentation update required labels Mar 21, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants