Array item removal binds a surviving sibling's sub-Field to a stale, out-of-range index for one render (stable/non-index keys)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 52/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- react, typescript
- 領域
- frontend
調査の方向性
@tanstack/form-core の配列ミューテーションとフィールドパスの導出から始め、ここで説明されている React AppField、安定したキー、form.getFieldValue API を使った再現手順を使用します。options から最初の項目を削除する動作を再現し、その後、残った sub-Fields が options[1] を保持する理由を追跡します。完了条件は、一時的な undefined 値なしで、シフト後のパスを使用することです。
索引モデルが issue の本文から書いたものです。
説明
[Opus-written, can add human replies if something's not clear or helpful!]
First off, thanks for all the work on TanStack Form. It's been a joy to build with, and this is a pretty narrow edge case rather than anything blocking us. Wanted to write it up carefully in case it helps.
Summary
When you remove a non-last item from an array field, a surviving sibling's sub-Fields re-render for one frame with their name still pointing at the old, now out-of-range index. During that single frame both field.state.value and form.getFieldValue(field.name) come back undefined, even though the field's value type is non-optional (for example a plain string). On the very next render everything reconciles to the correct index and is fine.
This shows up when the array item containers use stable (non-index) React keys. We need stable keys for drag-to-reorder and view transitions, so switching to index keys isn't really an option for us.
I can confirm it still reproduces on @tanstack/[email protected], which is a good number of releases after the earliest related report.
How this relates to existing issues
I dug through the tracker first, and two closed issues are in the neighborhood:
- #1363 ("Non-index keys in React causing inputs to be uncontrolled when removing array fields") looks like the same root cause. With non-index keys, removing an item leaves a sibling's field
name/path pointing at a stale index, the value reads asundefinedfor a render, and React flips the input to uncontrolled. It was last reproduced on 1.2.3 and closed without a fix landing, so I figured a fresh writeup on a current version might be useful. - #1518 is close but subtly different. There,
form.getFieldValue(field.name)still returns the correct value whilestate.valueis transientlyundefined. In our case that workaround doesn't hold, because the field'snameitself is stale, so the store lookup comes back empty too. The lag is in the field's path, not just its cached value.
Happy to be pointed at whichever of these you'd rather consolidate into if you feel it's a duplicate.
Versions
@tanstack/react-form1.28.3,@tanstack/form-core1.28.x- React 19, Next.js 16 (App Router)
Reproduction
- An array field
options, where each item is{ name: string, values: {...}[] }. - The item containers are keyed by a stable key (
key={opt.name}), because rows are drag-reorderable (dnd-kit) and animated with<ViewTransition>, both of which want stable identity. - Each row renders its sub-fields via
form.AppField name={options[${idx}].name}and similar, whereidxis the.map()index passed down as a prop.
Steps:
- Start with two options, say
Tamaño(index 0) andColor(index 1). - Remove the first one (
Tamaño, index 0). The array becomes length 1, andColorshould shift to index 0. - The surviving
Colorrow's sub-fields re-render once.
What I see
On that re-render, some temporary tracing logged (formatted for readability):
OptionHeader.name transient { fieldName: "options[1].name", stateValue: null, storeValue: null }
OptionBody.values transient { fieldName: "options[1].values", stateValue: null, storeValue: null }
fieldNameis stilloptions[1], the old index, even though the array now only hasoptions[0].field.state.valueisundefined.form.getFieldValue(field.name)is alsoundefined, becauseoptions[1]no longer exists.
The next render reconciles to options[0] and it's all correct.
What I'd expect
A sub-Field of an array item shouldn't render bound to an index that no longer exists after a sibling is removed, and a field whose value type is non-optional shouldn't transiently expose undefined.
What we're doing for now
We early-return when the value is missing, since the field is about to reconcile or unmount anyway:
<form.AppField name={`options[${optionIdx}].name`}>
{(nameField) => {
// Transient undefined guard, TanStack Form bug
// oxlint-disable-next-line typescript/no-unnecessary-condition
if (nameField.state.value === undefined) return null
// ...
}}
</form.AppField>
That avoids the crash and the final render is correct. The tradeoff is that the row renders nothing for one frame, and the typed value is briefly a small lie.
So
Would it be feasible for the array re-index to update a surviving item's sub-Field name/value in step with the array mutation (or to skip rendering removed or shifted paths), so that stable-keyed array items stay type-correct without forcing index keys? I know that's easier said than done given how derivation works, so no pressure. I'm glad to put together a minimal StackBlitz on 1.28.x if that would help you dig in.
- 主要言語
- TypeScript
- スター
- 6.8k
- フォーク
- 717
- 平均マージ
- 5日 22時間
- マージ済み PR(30日)
- 14
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
TanStack/form のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
Submit listener exceptions leave isSubmitting stuck true対応中かも @henricop が 14 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
handleSubmit calls onSubmit again while a submit is still pending (since 1.33.1)対応中かも @twotnguyen が 14 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
`isDefaultValue` is wrong when a field-level `defaultValue` is `null`対応中かも @fenet-as が 40 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
TanStack/form#2427 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Async validator doesn't re-trigger `isValidating` after first failed validation対応中かも @drewsephski が 44 日前に担当しました。 オープン
難易度 2/5 1〜2日 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
clawsweeper:needs-maintainer-review clawsweeper:needs-product-decision clawsweeper:no-new-fix-pr clawsweeper:source-repro impact:ux-friction issue-rating: 🦞 diamond lobster P3
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
openclaw/openclaw#203385 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
nwverse/persona#12800 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
メンテナーはふだん 1 日以内に返信
-
[MCP]: page snapshots in .playwright-mcp/ are not git-ignored (.playwright-cli/ is, since #42318)オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
microsoft/playwright#44289 ·
メンテナーはふだん 1 日以内に返信
-
good first issue
難易度 2/5 半日 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信