Skip to content
Source
Sign inSign up
TanStack/form

Fix Array Swap Detection #2172

Mergedcrutchcorn(GitHub profile, opens in a new tab)merged 5 commits intomain fromrefs/pull/2172/headmerged by crutchcorn(GitHub profile, opens in a new tab)

Fixes https://github.com/TanStack/form/issues/2018 for all adapters

Summary by CodeRabbit

  • Bug Fixes

    • Array form fields now properly re-render when array elements are reordered without changing the array length (e.g., swap, move operations).
  • Tests

    • Added test coverage verifying array field re-rendering behavior across all framework adapters.

Review Change Stack

    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      Caution

      Review failed

      Pull request was closed or merged during review

      📝 Walkthrough

      Walkthrough

      Introduces _arrayVersion counter to field metadata to track structural array changes. Increments this counter on array mutations (swap, move, insert, remove) via bumpArrayVersion helper. Updates all framework adapters—React, Preact, Vue, Svelte, Solid, and Angular—to watch this counter instead of array length for re-renders, ensuring components update when array values change without length changes.

      Changes

      Array Re-render on Value Change

      Layer / File(s)Summary
      Data Shape: Array Version Counter
      packages/form-core/src/FieldApi.ts, packages/form-core/src/metaHelper.ts, packages/form-core/tests/FieldApi.spec.ts
      FieldMetaBase adds _arrayVersion: number; defaultFieldMeta initializes it to 0; test updated to expect the field.
      Core Meta Helpers: Bump and Array Operations
      packages/form-core/src/metaHelper.ts
      Introduce bumpArrayVersion helper and integrate it into array operation handlers (handleArrayMove, handleArrayRemove, handleArraySwap, handleArrayInsert); update metaHelper return to expose bumpArrayVersion.
      FormApi: Initialize and Bump Array Version
      packages/form-core/src/FormApi.ts
      Initialize _arrayVersion during field meta transform; call bumpArrayVersion after array mutations in pushFieldValue, replaceFieldValue, and clearFieldValues.
      React & Preact Adapters: Array Version Reactivity
      packages/react-form/src/useField.tsx, packages/react-form/tests/useField.test.tsx, packages/preact-form/src/useField.tsx, packages/preact-form/tests/useField.test.tsx
      Update useField reactivity selector to track state.meta._arrayVersion instead of array length; add tests validating re-render on swap.
      Vue Adapter: Array Version Reactivity
      packages/vue-form/src/useField.tsx, packages/vue-form/tests/useField.test.tsx
      Update Vue useField reactivity selector to track state.meta._arrayVersion for array-mode re-renders; add test validating re-render on swap.
      Svelte Adapter: Array Version Reactivity
      packages/svelte-form/src/Field.svelte, packages/svelte-form/tests/array-swap.svelte, packages/svelte-form/tests/array.test.ts
      Update Svelte Field reactive storeSub to track state.meta._arrayVersion; add test component and test suite validating re-render on swap.
      Solid Adapter: Array Version Reactivity
      packages/solid-form/src/createField.tsx, packages/solid-form/tests/createField.test.tsx
      Update Solid createField reactivity selector to track state.meta._arrayVersion; add test validating re-render on swap.
      Angular Adapter: Array Version Reactivity
      packages/angular-form/src/tanstack-field.ts, packages/angular-form/tests/tanstack-field.spec.ts
      Update Angular TanStackField reactivity selector to track state.meta._arrayVersion; add test validating re-render on swap.
      Release Notes: Changeset Entry
      .changeset/stale-berries-drop.md
      Document patch version bump across @tanstack/*-form packages for array re-render fix.

      Sequence Diagram

      sequenceDiagram
        participant Component as Array Field Component
        participant FormApi as FormApi
        participant MetaHelper as metaHelper
        participant Store as Reactive Store
        Component->>FormApi: swapFieldValues(fieldName, idx1, idx2)
        FormApi->>MetaHelper: bumpArrayVersion(fieldName)
        MetaHelper->>FormApi: setFieldMeta(fieldName, {_arrayVersion: N+1})
        FormApi->>Store: emit meta change
        Store->>Component: state.meta._arrayVersion dependency fires
        Component->>Component: re-render

      Estimated code review effort

      🎯 3 (Moderate) | ⏱️ ~25 minutes

      Poem

      🐰 Arrays now dance without a care, When swapping values here and there— Version bumps track each little change, No length checks needed in the range! Six adapters march in fine array, Re-renders happen every day! ✨

      🚥 Pre-merge checks | ✅ 3 | ❌ 2

      ❌ Failed checks (1 warning, 1 inconclusive)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 61.54% which is insufficient. The required threshold is 80.00%.Write docstrings for the functions missing them to satisfy the coverage threshold.
      Description check❓ InconclusiveThe PR description is minimal and vague, only stating 'Fixes https://github.com/TanStack/form/issues/2018 for all adapters' without explaining what changes were made.Expand the description to explain what changes were made (introducing _arrayVersion meta tracking, updating adapters, etc.) and reference the issue details more comprehensively.
      ✅ Passed checks (3 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title 'Fix Array Swap Detection' accurately describes the main change: fixing array swap/move detection so fields re-render properly when array values are swapped.
      Linked Issues check✅ PassedThe PR fully addresses issue #2018 by implementing array version tracking via _arrayVersion meta counter, updating all adapter implementations (react, angular, preact, solid, svelte, vue) to use this counter instead of array length for change detection, and adding comprehensive tests for swap functionality across all adapters.
      Out of Scope Changes check✅ PassedAll changes are directly scoped to fixing array swap detection: new _arrayVersion tracking, adapter updates to use this counter, array operation handlers (swap, move, insert, remove), test coverage for swap behavior, and a changeset entry. No unrelated changes detected.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing Touches
      📝 Generate docstrings
      • Create stacked PR
      • Commit on current branch
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `fix-array-swap-detection`

      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 and usage tips.
    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 124ce20f7a9a709e02752dd480a61039782d6dab

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded50sView ↗
      nx run-many --target=build --exclude=examples/**✅ Succeeded9sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-05-10 22:25:15 UTC

    3. github-actions[bot](GitHub profile, opens in a new tab)commented

      🚀 Changeset Version Preview

      7 package(s) bumped directly, 7 bumped as dependents.

      🟨 Minor bumps

      PackageVersionReason
      @tanstack/angular-form1.31.0 → 1.32.0Changeset
      @tanstack/form-core1.31.0 → 1.32.0Changeset
      @tanstack/react-form1.31.0 → 1.32.0Changeset
      @tanstack/solid-form1.31.0 → 1.32.0Changeset
      @tanstack/svelte-form1.31.0 → 1.32.0Changeset
      @tanstack/vue-form1.31.0 → 1.32.0Changeset
      @tanstack/react-form-nextjs1.31.0 → 1.32.0Dependent
      @tanstack/react-form-remix1.31.0 → 1.32.0Dependent
      @tanstack/react-form-start1.31.0 → 1.32.0Dependent

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/preact-form1.29.6 → 1.29.7Changeset
      @tanstack/form-devtools0.2.26 → 0.2.27Dependent
      @tanstack/lit-form1.24.0 → 1.24.1Dependent
      @tanstack/react-form-devtools0.2.26 → 0.2.27Dependent
      @tanstack/solid-form-devtools0.2.26 → 0.2.27Dependent
    4. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/angular-form

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

      @tanstack/form-core

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

      @tanstack/form-devtools

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

      @tanstack/lit-form

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

      @tanstack/preact-form

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

      @tanstack/react-form

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

      @tanstack/react-form-devtools

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

      @tanstack/react-form-nextjs

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

      @tanstack/react-form-remix

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

      @tanstack/react-form-start

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

      @tanstack/solid-form

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

      @tanstack/solid-form-devtools

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

      @tanstack/svelte-form

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

      @tanstack/vue-form

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

      commit: adad277

    5. Codecov Report

      :white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 91.27%. Comparing base (6892ed0) to head (adad277). :warning: Report is 210 commits behind head on main.

      Additional details and impacted files
      @@            Coverage Diff             @@
      ##             main    #2172      +/-   ##
      ==========================================
      + Coverage   90.35%   91.27%   +0.91%     
      ==========================================
        Files          38       59      +21     
        Lines        1752     2314     +562     
        Branches      444      568     +124     
      ==========================================
      + Hits         1583     2112     +529     
      - Misses        149      181      +32     
      - Partials       20       21       +1     

      :umbrella: View full report in Codecov by Sentry. :loudspeaker: Have feedback on the report? Share it here.

      :rocket: New features to boost your workflow:

      • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
      • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign in to join this conversation.