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: watch the field in Controller with defaultValues and shouldUnregister: true return undefined #10096

Closed
1 task done
Hengry opened this issue Mar 13, 2023 · 2 comments · Fixed by #10102
Closed
1 task done
Labels
status: under investigation aware of this issue and pending for investigation

Comments

@Hengry
Copy link

Hengry commented Mar 13, 2023

Version Number

^7

Codesandbox/Expo snack

https://codesandbox.io/s/angry-microservice-l3z1ru?file=/package.json

Steps to reproduce

  1. Click on "console" tab
  2. See firstName undefined due to the latest render.

Expected behaviour

The result should be firstName a.

Either delete shouldUnregister: true or using register instead of controller would behave normally, but in my case, Controller and shouldUnregister: true are both necessary.

What browsers are you seeing the problem on?

No response

Relevant log output

No response

Code of Conduct

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

I think this issue rather difficult/impossible to resolve due to react strict mode with double useEffect invoke, we are relying on useEffect to determine whether input unmounts or not, I will take another look, but most likely this will not be solved.

@bluebill1049 bluebill1049 added the status: under investigation aware of this issue and pending for investigation label Mar 13, 2023
@Hengry
Copy link
Author

Hengry commented Mar 14, 2023

@bluebill1049
I also found another case having similar behavior with different root cause.
If Controller was used with conditional rendering and watch was also called in the same component (with shouldUnregister: true), then the default value would also missing in the return of watch. Not like the previous report, this one would happened even turning off the StrictMode. While using useController instead of Controller would solve this.

I add these case in the same codesandbox,

Codesandbox/Expo snack

https://codesandbox.io/s/angry-microservice-l3z1ru?file=/src/index.tsx

Steps to reproduce

  1. Switch to index.tsx.
  2. Remove StrictMode.
  3. Switch to App.tsx.
  4. Click on "open" button in the browser.
  5. The console.log would show in the console tab.

Expected behaviour

The result should be

firstName: a 
controllerInput: controller input
useControllerInput: useController input

Relevant log output

firstName: a 
controllerInput: undefined
useControllerInput: useController input

bluebill1049 added a commit that referenced this issue Mar 14, 2023
* close #10096 react strict mode with mounted field value

* fix lint error
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: under investigation aware of this issue and pending for investigation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants