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

<Controller> with onFocus erroneously reregisters itself #1525

Closed
donocode opened this issue Apr 28, 2020 · 3 comments
Closed

<Controller> with onFocus erroneously reregisters itself #1525

donocode opened this issue Apr 28, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@donocode
Copy link

donocode commented Apr 28, 2020

Describe the bug
When using a component with an onFocus callback it reregisters itself after validation failure (or if focused in react dev tools)

To Reproduce
Steps to reproduce the behavior:

  1. Go to codesandbox link
  2. Type some text in FirstName and click submit (don't add text to LastName)
  3. See how the FirstName was reset
  4. See how you can no longer type in FirstName

Codesandbox link (Required)

https://codesandbox.io/s/controller-issue-6gr5t?file=/src/index.js

Expected behavior
Having an onFocus callback should not cause the input to reregister on validation failure

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: OSX High Sierra
  • Browser: Chrome
  • Version: 81.0.4044.113

Additional context
The reregistering problem will also occur if you focus the Controller in react dev tools. If you have multiple Controllers in the form with onFocus callbacks then they will all exhibit the same behaviour

@bluebill1049 bluebill1049 added the bug Something isn't working label Apr 28, 2020
@JeromeDeLeon
Copy link
Contributor

JeromeDeLeon commented Apr 29, 2020

The reason for this is because your forgot to memoize your function.

In Line 107 of Controller, notice that the onFocus is part of useCallback's dependencies of registerField hence re-register happen because onFocus handler is creating new reference for every render.

@JeromeDeLeon
Copy link
Contributor

@bluebill1049, should we handle it inside or let user defines it?

@bluebill1049
Copy link
Member

wow, awesome find! @JeromeDeLeon it's awesome to see you are taking over the Controller component! I think we should memo in the lib.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants