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

Bug: Unmount of non-primitive field immediately after reset causes error #5492

Closed
rosemariesadler opened this issue Jun 4, 2021 · 6 comments
Labels
bug Something isn't working

Comments

@rosemariesadler
Copy link
Contributor

Describe the bug
When the value of a field is not a primitive (i.e. we have some meta-data that we save into the form for a typeahead component), if the field is unmounted immediately after reset an error occurs: cannot set property 'mount' of undefined. registerAbsentFields treats the object like each layer is its own key, causing _f to be undefined on get(fieldsRef.current, name) during the cleanup function of useController's useEffect.

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/s/restless-tree-j60ws
  2. Click on "Toggle Metadata" so the field is showing
  3. Click on "Reset", which resets the form and hides the field
  4. See error

Codesandbox link (Required)
https://codesandbox.io/s/restless-tree-j60ws

Expected behavior
No error should occur when the field is unmounted immediately after reset.

Desktop (please complete the following information):

  • OS: macOS Catalina
  • Browser: Chrome
  • Version 91

Smartphone (please complete the following information):

  • Device: Pixel 3
  • OS: Android 11
  • Browser: Chrome
  • Version: 91

Additional context
I'm happy to put out a PR to fix the issue, but I'm not certain of the preferred approach. The most obvious fast fix I see is adding the check for _f existence in the cleanup function, but I'm wondering if more support should be added for non-primitive values? Maybe a ref that keeps track of the names of registered fields that we would then reference during registerAbsentFields instead of just checking if the field is a primitive value?

@bluebill1049 bluebill1049 added the bug Something isn't working label Jun 4, 2021
@bluebill1049 bluebill1049 self-assigned this Jun 4, 2021
@rosemariesadler
Copy link
Contributor Author

Thank you for the quick resolution! I appreciate the great work you've done on this library and the good support you provide!

@bluebill1049 bluebill1049 removed their assignment Jun 4, 2021
@bluebill1049
Copy link
Member

Welcome @rosemariesadler 🙉

@Deepak7985
Copy link

@bluebill1049 I'm getting the same bug again in the new version 7.30.0 , I saw few lines around that useEffect has changed c41511f ??

@Deepak7985
Copy link

In the Commit c41511f we can see there is a check on field._f but in the latest version we don't have that check.

@tetchel
Copy link

tetchel commented May 26, 2022

I have a different bug with a similar error message. It seems to be happening on a component which is being unmounted after the form submits.

I tried versions 7.29.0, 7.30.0, and 7.31.2 and have the same result on all 3.

Uncaught TypeError: Cannot set properties of undefined (setting 'mount')
    at updateMounted (useController.ts:81:9)
    at useController.ts:97:11
    at safelyCallDestroy (react-dom.development.js:22831:5)
...

react_devtools_backend.js:4026 The above error occurred in the <Controller> component:
    at Controller (http://localhost:3000/static/js/bundle.js:197532:35)
    at <my component> (http://localhost:3000/static/js/bundle.js:16737:20)

@tetchel
Copy link

tetchel commented May 26, 2022

I had a wrapper component which was passed the name to provide to the controlled component.

for some reason, changing the object so that the name did not contain any periods (ie, the object was not nested) fixed this bug.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 10, 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

4 participants