Skip to content

[v2] Fix: Keep ArrayField in sync after array replacement - #2356

Open
scttcper wants to merge 2 commits into
TanStack:alphafrom
scttcper:scttcper/arrayfield-sync
Open

[v2] Fix: Keep ArrayField in sync after array replacement#2356
scttcper wants to merge 2 commits into
TanStack:alphafrom
scttcper:scttcper/arrayfield-sync

Conversation

@scttcper

@scttcper scttcper commented Aug 24, 2026

Copy link
Copy Markdown

ArrayField only reacts to array length and _arrayVersion, so replacing an array with a different array of the same length updated form state without rerendering the array container. UI rendered from the array could stay stale while submission used the new values.

This bumps _arrayVersion for direct same-length replacements. Array helpers now rely on length changes or this shared behavior instead of manually updating the version, while nested field updates still avoid rerendering the parent ArrayField.

Summary by CodeRabbit

  • Bug Fixes

    • Array fields now update reliably when replaced with a new array of the same length.
    • Array components rerender when their array value changes, while avoiding unnecessary rerenders for nested field updates.
    • Array operations such as swapping, moving, or clearing items now maintain consistent field updates.
  • Tests

    • Added coverage for array replacement, updater functions, nested updates, and array helper operations.

Replacing an array with another array of the same length updated form state without rerendering ArrayField.

Bump the shared array version for direct replacements while keeping nested field updates isolated.

Co-Authored-By: OpenAI Codex <noreply@openai.com>
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 7561444a-d36e-46ce-a636-be889ad3dab7

📥 Commits

Reviewing files that changed from the base of the PR and between 4ad5a7b and 4f0849d.

📒 Files selected for processing (4)
  • packages/form-core/src/FormApi/FormApi.lib.ts
  • packages/form-core/src/FormApi/array-methods.lib.ts
  • packages/form-core/tests/FormApi/field-state.spec.ts
  • packages/react-form/tests/FormField.spec.tsx
💤 Files with no reviewable changes (1)
  • packages/form-core/src/FormApi/array-methods.lib.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The change centralizes _arrayVersion updates in setFieldValue, removes duplicate notifications from array helpers, and adds core and React tests for array replacement, nested updates, helper operations, and ArrayField rerender behavior.

Changes

Array version tracking

Layer / File(s) Summary
Centralize array-version updates
packages/form-core/src/FormApi/FormApi.lib.ts, packages/form-core/src/FormApi/array-methods.lib.ts
setFieldValue resolves updater values before writing them and increments _arrayVersion for same-length array replacements. Array helpers no longer increment the metadata directly.
Validate array version behavior
packages/form-core/tests/FormApi/field-state.spec.ts, packages/react-form/tests/FormField.spec.tsx
Tests cover direct and updater replacements, nested field updates, array helpers, and ArrayField rerender behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 4f084

The change makes equal-length array replacements rerender correctly while preserving nested field behavior and is merge-ready after normal checks and review; no actionable merge-blocking risk remains.

Suggested reviewers: lecarbonator

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description clearly explains the problem, motivation, and implementation. It omits the required Checklist and Release Impact sections from the repository template. Add the Checklist section and mark the applicable items. Add the Release Impact section and indicate whether the change requires a changeset or is docs/CI/dev-only.
Docstring Coverage ⚠️ Warning 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 2 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: keeping ArrayField synchronized after array replacement.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

expect(input).toHaveValue('new-value')
})

it('rerenders an ArrayField for a same-length replacement', () => {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test does fail without the change

@scttcper
scttcper marked this pull request as ready for review August 24, 2026 15:20
@nx-cloud

nx-cloud Bot commented Aug 27, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit e409b42

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ⛔ Cancelled 16s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 24s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-27 10:22:14 UTC

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.

1 participant