Skip to content

fix(react-form): use fresh FieldApi in current render on name change#2240

Open
xianjianlf2 wants to merge 1 commit into
TanStack:mainfrom
xianjianlf2:fix/keyed-field-stale-on-array-removal-2238
Open

fix(react-form): use fresh FieldApi in current render on name change#2240
xianjianlf2 wants to merge 1 commit into
TanStack:mainfrom
xianjianlf2:fix/keyed-field-stale-on-array-removal-2238

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Jul 11, 2026

Copy link
Copy Markdown

Closes #2238

Regression from #1893 (v1.27.0). On a name/form change, useField calls setFieldApi(new FieldApi(...)) during render but the current render keeps using the stale fieldApi state. React discards that render, yet the render prop still runs once against the stale FieldApi's store — which derives undefined for an array index that no longer exists after a removal, crashing strict render code.

Fix applies the canonical "adjust-state-during-render + local variable" pattern: build the new instance, use it for the rest of the current render, and still setFieldApi it (keeping the setState identity #1893 introduced for React Compiler safety) — restoring pre-1.27.0 correctness.

  • Added a test that keys array items by stable id, removes a non-last item, and asserts the subfield never observes undefined; fails before, passes after.
  • pnpm vitest run (react-form) → 126 passed, no type errors.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed field state updates when form or field names change during rendering.
    • Prevented array-field values from becoming undefined when removing non-last items.
    • Preserved the correct values and displayed content for remaining array items.
  • Tests

    • Added regression coverage for stable, keyed array fields when items are removed.

When a field's `name` changes (e.g. a non-last item is removed from a
stably-keyed array), `useField` created a new FieldApi via
`setFieldApi` during render but continued the current render pass with
the stale instance. React discards that pass, but the render prop still
runs to completion once with the stale FieldApi, whose store derives
`undefined` for the path that no longer exists at the old index. This
briefly surfaces `state.value === undefined` for surviving array items,
crashing render code that treats the value strictly.

Adjust state during render but also use the freshly created FieldApi for
the remainder of this render, so the field reads state at its current
`name`. Keeps the setState-based identity (React Compiler safe) while
restoring the pre-1.27.0 behavior.

Closes TanStack#2238
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9b3f1045-15cb-444c-a4c7-36366e3181dd

📥 Commits

Reviewing files that changed from the base of the PR and between 397ab97 and 09b74ac.

📒 Files selected for processing (2)
  • packages/react-form/src/useField.tsx
  • packages/react-form/tests/useField.test.tsx

📝 Walkthrough

Walkthrough

useField now uses a newly created FieldApi immediately when its form or name changes. A regression test verifies that removing the first item from a stably keyed array preserves the shifted field value without transient undefined state.

Changes

FieldApi synchronization

Layer / File(s) Summary
Synchronous FieldApi replacement and regression coverage
packages/react-form/src/useField.tsx, packages/react-form/tests/useField.test.tsx
useField uses the replacement FieldApi during the current render and stores that same instance in state. The test verifies correct values and no undefined observations after removing a non-last keyed array item.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix: using a fresh FieldApi during render when the field name changes.
Description check ✅ Passed The description explains the bug, fix, regression test, and verification results, though it omits the template's checklist and release-impact sections.
Linked Issues check ✅ Passed The changes address #2238 by using the fresh FieldApi in-render and adding a regression test for uuid-keyed array removal.
Out of Scope Changes check ✅ Passed The modified files stay focused on the reported useField regression and its test coverage, with no unrelated changes evident.
✨ 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.

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.

Fields in uuid-keyed array items transiently go undefined on removal again: #1729 fixed it in 1.26.0, #1893 regressed it in 1.27.0

1 participant