🐞 fix #10139 useFieldArray
array error not updating in some cases
#10150
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes issue #10139, which causes the error state of a
useFieldArray
array to not update on append/remove/etc. actions on the array if any of the fields of the array already had an error. It also fixes another issue I discovered while testing, which is that the error will not update from one error to another.The issue is caused by this code:
react-hook-form/src/useFieldArray.ts
Lines 328 to 330 in 20d897b
which appears to be an optimization to avoid unnecessarily setting or unsetting the error state of the array after a resolver validates. However, this check does not account for the possibility of going from no error to error (or vice versa) while one of the fields already has an error, nor does it account for the array error changing from one error directly to another.
I fixed the issue by simply removing the
if
statement that attempts to avoid setting or unsetting the error state. This will make the error state update after every resolver validation. It is possible that this may hurt performance in some cases, but the Git history does not show that the check was added for this reason.I added a test that ensures these issues are resolved.