fix(react-form): [v2] rerender `ArrayField` when the array's own errors change #2444
alpha fromrefs/pull/2444/head·opened 🎯 Changes#
An ArrayField doesn't show an error from its own validator while the array's length stays the same. With validators={[{ triggers: [], run: ({ value }) => value.length < 2 ? 'Add another item' : undefined }]} and children that render field.errors, nothing renders after submit, though the form is invalid. Preact does the same.
useArrayFieldSubscription selects only value.length and meta._arrayVersion, so a change to the array's own errors doesn't rerender it. Preact's copy is the same. Vue has kept the ArrayField meta reactive since #2401. Solid has the same gap and isn't touched here.
The hook now also selects state.meta.errors with compare: shallow, as Subscribe does. It has to be shallow: while errorVisibility hides errors, derived meta gets a fresh [] on every update, and an identity compare rerendered the ArrayField on every child edit. Child edits still don't rerender it. I left out isValidating: a child edit runs the array's own validation and flips its isSelfValidating even with a submit-only validator, so the ArrayField would rerender on every keystroke.
Each adapter has a test in FormField.spec.tsx: typing into a child with an async validator leaves the ArrayField's render count unchanged, and submit renders the array's error. On alpha it fails at the submit step (React normal and strict, Preact):
FAIL |@tanstack/react-form: normal (chromium)| tests/FormField.spec.tsx:144 > Form fields > rerenders an ArrayField when its own errors change
Error: expect(element).toHaveTextContent()
Expected element to have text content:
Add another item
Received:
With the fix, react-form (100 tests) and preact-form (42 tests) pass, tsc shows no new errors, and pnpm test:pr passes locally (28 projects affected).
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Array fields now update to display their own validation errors when those errors change, including after form submission.
- Changes to nested field validation errors do not trigger unnecessary array field rerenders.
- 1
No actionable comments were generated in the recent review.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/form
- Branch: alpha
- File: .coderabbit.yaml
- Commit: 3545d8c
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
d83a8a59-461a-42e2-9903-962b1ad149b4
Commits-
Reviewing files that changed from the base of the PR and between 3545d8cb718a737af551594cbfb61abdcbece1a6 and 478c73894daf3b14c09bb9a44dd8d78702fe1a37.
Files selected for processing: 5-
.changeset/loud-arrays-report.mdpackages/preact-form/src/PreactForm/fieldSubscriptions.lib.tspackages/preact-form/tests/FormField.spec.tsxpackages/react-form/src/ReactForm/fieldSubscriptions.lib.tspackages/react-form/tests/FormField.spec.tsx
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
Walkthrough
-
React and Preact array-field subscriptions now track changes to the array’s own errors. Tests verify the array field updates for its validation error without rerendering when only a nested field’s error changes.
Changes: ArrayField error subscriptions
Priority:Layer / File(s) Summary Subscribe to array-level errors
packages/react-form/src/ReactForm/fieldSubscriptions.lib.ts,packages/preact-form/src/PreactForm/fieldSubscriptions.lib.tsBoth implementations select meta.errorswith shallow comparison and include the selected errors in memo dependencies.Verify error-driven rerender behavior
packages/react-form/tests/FormField.spec.tsx,packages/preact-form/tests/FormField.spec.tsx,.changeset/loud-arrays-report.mdTests check that nested-field errors do not rerender the array field and that its own validation error appears after submission. The changeset records a patch for both packages. NormalEstimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Suggested reviewers:
lecarbonator,crutchcorn
Merge Risk:
Minimal · up to478c7-
The ArrayField subscription change has no identified issue that needs resolution before merge.
Pre-merge checks:
4 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Docstring Coverage Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 4 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold. Passed checks: 4 passed-
Check name Status Explanation Title check The title names the affected adapter (react-form), the component (ArrayField), and the behavior fixed (rerender when the array's own errors change). The changeset covers this main change. The title al… Description check The description follows the template. It has a Changes section that explains the bug, the cause in useArrayFieldSubscription, the chosen fix (shallow compare on state.meta.errors), and why isValidatin… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Title check
-
Explanation
The title names the affected adapter (react-form), the component (ArrayField), and the behavior fixed (rerender when the array's own errors change). The changeset covers this main change. The title also lists the [v2] tag, which adds context, not noise. The PR also changes preact-form, which the title does not name. That omission is minor because the title covers the main change.
Full details: Description check
-
Explanation
The description follows the template. It has a Changes section that explains the bug, the cause in useArrayFieldSubscription, the chosen fix (shallow compare on state.meta.errors), and why isValidating was left out. It gives a reproduction and a test description, and it lists local test results. All three checklist items are ticked. The Release Impact section marks the changeset as generated, and the diff includes a changeset file. The description does not cover the Solid gap in detail, but it states that Solid is not changed.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 4 files. (1 skipped: 1 unsupported.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands.-
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 478c73894daf3b14c09bb9a44dd8d78702fe1a37
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR