Unkeyed siblings after a .map() list are remounted when the list changes length (React keeps them)
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 48/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- react, typescript
- 领域
- frontend
调研方向
Start with pushChildren in dist/dom/reconcile.js and trace how reconcile() assigns keys to children from nested arrays. Run the provided Vitest and jsdom reproduction, then compare behavior for a changing list and for two lists in one parent. Done means later unkeyed siblings retain their DOM nodes and state, while each nested list has its own key scope, matching the React behavior described.
由索引模型根据 Issue 内容生成。
描述
Version: @tanstack/redact 0.3.0 (also 0.2.6)
When a .map() list changes length, Redact recreates every unkeyed sibling that comes after the list in the same parent: new DOM nodes, fresh component state, lost focus and lost uncontrolled input values. React keeps them, because it treats each child array as a single slot.
It happens for [a] -> [a, b], [a, b] -> [a] and undefined -> []. Swapping null for a one-item array does not trigger it.
Repro (Vitest + jsdom + Testing Library)
import { useState } from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import { expect, test } from 'vitest';
test('an unkeyed sibling after a list keeps its DOM node when the list grows', () => {
function Form() {
const [items, setItems] = useState(['a']);
return (
<div>
{items.map((item) => <p key={item}>{item}</p>)}
<input aria-label="field" />
<button onClick={() => setItems(['a', 'b'])}>add</button>
</div>
);
}
render(<Form />);
const field = screen.getByLabelText('field');
field.value = 'typed';
fireEvent.click(screen.getByText('add'));
expect(screen.getByLabelText('field')).toBe(field); // React: same node. Redact: new node
expect(screen.getByLabelText('field').value).toBe('typed'); // Redact: ''
});
React 19.2.8 passes; Redact 0.3.0 fails both assertions.
Where it seems to come from
pushChildren in dist/dom/reconcile.js flattens nested arrays into the parent's child list, and reconcile() then matches unkeyed children by their index in that flat list (child.key ?? i). When the array's length changes, every later unkeyed sibling shifts index and is unmounted and remounted. A side effect is that two .map() lists in one parent also share a key space, which React keeps separate.
Treating each nested array as an implicit fragment (one slot with its own key scope) would match React.
Real-world impact
We hit it in a settings form: a validation warnings list (warnings?.map(...)) went from undefined to [] on Save, and every field below it was rebuilt, so the field the user was typing in lost its value and focus.
Possibly related to the "separate Fragment identity bug" mentioned in #92.
- 主要语言
- TypeScript
- 星标
- 581
- 派生
- 28
- 平均合并
- 13 小时 48 分钟
- 30 天内合并 PR
- 45
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/redact 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 4/5 1-2 天 新手友好度 35/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 20/100
维护者通常 1 天内回复
相似的 Issue
-
inference/go type/feature
难度 1/5 1-3 小时 新手友好度 72/100
anomalyco/opencode#65148 · 1 条评论 ·
维护者通常 1 天内回复
-
truapi-release
难度 2/5 1-3 小时 新手友好度 72/100
paritytech/product-sdk#529 ·
维护者通常 1 天内回复
-
Add: Mansudae TV未关闭approved check:passed streams:add
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
-
[Perf] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 77/100
devlikeapro/waha#2369 ·
维护者通常 5 天内回复