Unkeyed siblings after a .map() list are remounted when the list changes length (React keeps them)

未关闭
#149 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。