Skip to content

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

Merged
LeCarbonator merged 3 commits into
TanStack:alphafrom
scttcper:scttcper/arrayfield-sync
Sep 14, 2026
Merged

[v2] Fix: Keep ArrayField in sync after array replacement#2356
LeCarbonator merged 3 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.
    • Swapping, moving, and clearing array items now maintain consistent field updates.
  • Tests

    • Added coverage for array replacement through direct values and updater functions, nested updates, and array helper operations.
    • Added coverage confirming correct rerender behavior for array fields.

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 StackReview 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: Advanced

Run ID: 622331c0-48d2-4b0f-838e-d56c8817f8c6

📥 Commits

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

📒 Files selected for processing (1)
  • packages/form-core/src/FormApi/FormApi.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 updates from array helpers, and adds core and React tests for 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.

Priority: ⬇️ Low

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

Change: Bug fix

Suggested reviewers: lecarbonator

Merge Risk: ⚪ Minimal · up to 17d23

Array replacements and nested updates retain the intended rendering behavior, so the change is ready to merge after normal checks.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the code change and its motivation, but it does not use the required Changes, Checklist, or Release Impact sections. It also does not confirm the required local tests or chang… Update the description to include the required ## 🎯 Changes, ## ✅ Checklist, and ## 🚀 Release Impact sections. Complete the checklist items and state whether a changeset is required or whether the change 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 describes the primary change: keeping ArrayField synchronized after same-length 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.
Full details: Description check

Explanation

The description explains the code change and its motivation, but it does not use the required Changes, Checklist, or Release Impact sections. It also does not confirm the required local tests or changeset status.

  • 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.

Comment thread packages/react-form/tests/FormField.spec.tsx
@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 17d23c9

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 4m 38s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 23s View ↗

☁️ Nx Cloud last updated this comment at 2026-09-14 11:51:19 UTC

@LeCarbonator LeCarbonator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Noticed a small correctness bug, but apart from that, looks good! It might change a bit in the future to make the work of the adapters a bit easier, but that's not a concern atm. Thanks for the fix!

Comment thread packages/form-core/src/FormApi/FormApi.lib.ts Outdated
@pkg-pr-new

pkg-pr-new Bot commented Sep 14, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@2356

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@2356

@tanstack/form-devtools

npm i https://pkg.pr.new/@tanstack/form-devtools@2356

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@2356

@tanstack/preact-form

npm i https://pkg.pr.new/@tanstack/preact-form@2356

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@2356

@tanstack/react-form-devtools

npm i https://pkg.pr.new/@tanstack/react-form-devtools@2356

@tanstack/react-form-nextjs

npm i https://pkg.pr.new/@tanstack/react-form-nextjs@2356

@tanstack/react-form-start

npm i https://pkg.pr.new/@tanstack/react-form-start@2356

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@2356

@tanstack/solid-form-devtools

npm i https://pkg.pr.new/@tanstack/solid-form-devtools@2356

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@2356

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@2356

commit: 17d23c9

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (alpha@4ad5a7b). Learn more about missing BASE report.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff            @@
##             alpha    #2356   +/-   ##
========================================
  Coverage         ?   95.20%           
========================================
  Files            ?      113           
  Lines            ?     4383           
  Branches         ?      990           
========================================
  Hits             ?     4173           
  Misses           ?      200           
  Partials         ?       10           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@LeCarbonator
LeCarbonator merged commit 6e5b809 into TanStack:alpha Sep 14, 2026
9 checks passed
@scttcper
scttcper deleted the scttcper/arrayfield-sync branch September 14, 2026 15:04
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.

3 participants