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 returns undefined when rendering more than twice and useForm set shouldUnregister to true #11322

Closed
1 task done
cuongle-hdwebsoft opened this issue Dec 20, 2023 · 4 comments
Labels
status: under investigation aware of this issue and pending for investigation

Comments

@cuongle-hdwebsoft
Copy link

cuongle-hdwebsoft commented Dec 20, 2023

Version Number

7.49.2

Codesandbox/Expo snack

https://codesandbox.io/p/devbox/vibrant-neumann-7qhjwf?file=%2Fsrc%2FApp.tsx%3A85%2C1

Steps to reproduce

  1. Open the console in code sandbox
  2. Confirm that it logs
primaryColor red
unRenderField watch unRenderField
unRenderField useWatch unRenderField
  1. Click button
  2. The log will show
primaryColor red
unRenderField watch undefined
unRenderField useWatch unRenderField
  1. Continue step 3 and you will see unRenderField watch always returns undefined

Expected behaviour

The value of watch and useWatch should be returned consistently. So the result after click should be

primaryColor red
unRenderField watch unRenderField
unRenderField useWatch unRenderField

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 bluebill1049 added the status: under investigation aware of this issue and pending for investigation label Dec 22, 2023
@radoslavkarlik
Copy link

Happens with 7.48.2 as well.

@Moshyfawn
Copy link
Member

The expected behaviour is for both watch and useWatch to emit undefined as unRenderField is been unmounted and shouldUnregister is set to true.

Although the subscription code part of useWatch is a bit confusing to me, I was able to verify that the next function is never run due to how the subject is being tracked here for some reason. I updated the relevant reproduction code to force an HMR re-render, which caused useWatch to return the correct undefined value. Regarding the fix, I would appreciate your insight @bluebill1049 on what might be wrong with this useSubscribe call 😓

@Moshyfawn
Copy link
Member

It feels like the solution is to notify subscribers in _removeUnmounted, but that causes additional re-renders which I'm not sure are "healthy".. 🤔

_subjects.values.next({
  values: { ..._formValues },
})

@bluebill1049
Copy link
Member

can we please send some effort clean up the codesandbox when report issue? Make it easier for us maintainer to look at the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
rafaelcalhau pushed a commit to rafaelcalhau/react-hook-form that referenced this issue May 5, 2024
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

No branches or pull requests

4 participants