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: cleanup process in useController crashes when used with useFieldArray in certain cases #11937

Closed
1 task done
nakaakist opened this issue May 25, 2024 · 0 comments · Fixed by #11938
Closed
1 task done

Comments

@nakaakist
Copy link
Contributor

nakaakist commented May 25, 2024

Version Number

7.51.5

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/react-hook-form-null-check-zqxx7v?file=%2Fsrc%2FApp.js

Steps to reproduce

  1. Go to the codesandbox.
  2. Click on "remove 0th elem from array (will cause error)" button
  3. See error

Expected behaviour

The error should not occur.

The cause of the error is as follows:

  • In the CodeSandbox, a form with the default value { arr: [{ k: 0 }, { k: 1 }] } is rendered as follows:
    • The 0th element of the arr field is rendered by the <Zeroth /> component where the "arr.0" path is controlled by useController.
    • The 1st element of the arr field is rendered by <First /> component where the "arr.1.k" path is controlled by useController.
  • When the "remove 0th elem" button is clicked:
    1. The 0th element is removed from the form's control._fields. Here, the resulting control._fields is { arr: [{ k: { _f: ... }] } because arr.1.k is controlled by useController.
    2. Both <Zeroth /> and <First /> components start to unmount.
    3. When unmounting the <Zeroth /> component, the cleanup process in useController for the "arr.0" path is executed. Here, this cleanup expects control._fields to be { arr: [{ k: ..., _f: ... }, ...] }. However, as mentioned above, the _f field does not exist.
    4. This causes the app to crash.

Although the current example is too trivial to use useController (just the return values of useFieldArray are enough), in a complex form with heavily nested form values, using useController with useFieldArray can sometimes be more convenient.

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
@nakaakist nakaakist changed the title issue: cleanup process in useController crashes iwhen used with useFieldArray in certain cases issue: cleanup process in useController crashes when used with useFieldArray in certain cases May 25, 2024
nakaakist added a commit to nakaakist/react-hook-form that referenced this issue May 25, 2024
nakaakist added a commit to nakaakist/react-hook-form that referenced this issue May 25, 2024
bluebill1049 added a commit that referenced this issue May 25, 2024
…r` (#11938)

* fix #11937 add validation in cleanup in useController

* Update src/useController.ts

---------

Co-authored-by: Beier (Bill) <bluebill1049@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant