You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
issue: useFieldArray's remove method does not work as expected after setValue with shouldValidate: true when reading state from the getFieldState
#11473
See that the second field is not removed, and the first input changed its value to the same as the second field has
Go to the Input.tsx file
Comment the line 17
Click on the "RESET" button near the first field
See that removing is working as expected
Uncomment the line 17
Go to the Fields.tsx file
Remove shouldValidate: true from the setValue options on the line 45
Click on the "RESET" button near the first field
See that removing is working as expected
Expected behaviour
The expected behavior is a successful removal of the first input on the reset button click no matter whether the getFieldState or shouldValidate: true is present. There is a useEffect in the Fields component that calls the remove method from useFieldArray for the fields with empty string value, except the last field. It runs each render because since the watch function returns the same reference for the object with values when the field value is changed with setValue, we can't include values to the dependencies.
I can't replace the getFieldState with the fieldState from the useController because this code is a simplification of the code from the lib I use that has a wrapper around the useController, and the getFieldState is called inside some custom hook. Also, I can't replace the setValue with replace inside the reset button click handler since the Input component is used in other places, and the reset button logic should not be connected with useFieldArray.
This discussion was converted from issue #11465 on February 02, 2024 22:08.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Version Number
7.50.0
Codesandbox/Expo snack
https://codesandbox.io/p/sandbox/suspicious-wood-ym78nz
Steps to reproduce
Input.tsx
fileFields.tsx
fileshouldValidate: true
from thesetValue
options on the line 45Expected behaviour
The expected behavior is a successful removal of the first input on the reset button click no matter whether the
getFieldState
orshouldValidate: true
is present. There is auseEffect
in theFields
component that calls theremove
method fromuseFieldArray
for the fields with empty stringvalue
, except the last field. It runs each render because since thewatch
function returns the same reference for the object with values when the field value is changed withsetValue
, we can't includevalues
to the dependencies.I can't replace the
getFieldState
with thefieldState
from theuseController
because this code is a simplification of the code from the lib I use that has a wrapper around theuseController
, and thegetFieldState
is called inside some custom hook. Also, I can't replace thesetValue
withreplace
inside the reset button click handler since theInput
component is used in other places, and the reset button logic should not be connected withuseFieldArray
.What browsers are you seeing the problem on?
Chrome
Relevant log output
No response
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions