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

🐞 fix #10139 useFieldArray array error not updating in some cases #10150

Merged

Conversation

kylemclean
Copy link
Contributor

@kylemclean kylemclean commented Mar 21, 2023

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:

if (
existingError ? !error && existingError.type : error && error.type
) {

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.

@codesandbox
Copy link

codesandbox bot commented Mar 21, 2023

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@kylemclean kylemclean changed the title Fix #10139 useArrayField array error not updating in some cases Fix #10139 useFieldArray array error not updating in some cases Mar 21, 2023
@bluebill1049 bluebill1049 changed the title Fix #10139 useFieldArray array error not updating in some cases 🐞 fix #10139 useFieldArray array error not updating in some cases Mar 21, 2023
@bluebill1049
Copy link
Member

thanks @kylemclean for looking into this issue and fix.

@bluebill1049 bluebill1049 merged commit c1cb992 into react-hook-form:master Mar 21, 2023
6 checks passed
bluebill1049 added a commit that referenced this pull request Mar 30, 2023
bluebill1049 added a commit that referenced this pull request Apr 1, 2023
bluebill1049 added a commit that referenced this pull request Apr 1, 2023
…10216)

* Revert "Revert "🐞 fix #10139 `useFieldArray` array error not updating in some cases (#10150)""

This reverts commit bcee639.

* fix #10139 with errors diff from previous with field array action
kylemclean pushed a commit to kylemclean/react-hook-form that referenced this pull request Feb 2, 2024
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 this pull request may close these issues.

None yet

2 participants