Bug: Unmount of non-primitive field immediately after reset causes error
See original GitHub issueDescribe 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:
- Go to https://codesandbox.io/s/restless-tree-j60ws
- Click on “Toggle Metadata” so the field is showing
- Click on “Reset”, which resets the form and hides the field
- 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?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Thank you for the quick resolution! I appreciate the great work you’ve done on this library and the good support you provide!
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.