Two sibling .map() lists with the same keys leave orphaned DOM nodes that pile up on refill (React keeps key scopes per array)

オープン
#192 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
58/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
react, typescript
領域
frontend, testing

調査の方向性

Start by reading the child reconciliation code and the related issue #149, which the report identifies as likely sharing the same key-space problem. Run the supplied Vitest and Testing Library repro against React and Redact, then trace how sibling .map() arrays are represented during removal and refill. Done means both lists empty to [0, 0] and refill to [2, 2], with a regression test covering the case.

索引モデルが issue の本文から書いたものです。

説明

Version: @tanstack/redact 0.3.1. Baseline: react / react-dom 19.3.1.

When two .map() lists in the same parent use the same keys, emptying them leaves one list's DOM nodes in the document. Redact never removes them, and they pile up: each refill adds a full new set next to the stale nodes. React keeps each array's keys separate, so its output follows the props.

Sharing keys between sibling lists is valid React and easy to hit. For example, a map overlay might render one marker per item and then one label per group, where a single-item group's key is that item's key.

Repro (Vitest + happy-dom + Testing Library)
import { render } from '@testing-library/react';
import { expect, test } from 'vitest';

function Two({ ids }) {
  return (
    <div>
      {ids.map((id) => <span key={id} className="a" />)}
      {ids.map((id) => <span key={id} className="b" />)}
    </div>
  );
}

test('two sibling lists with the same keys empty and refill like React', () => {
  const view = render(<Two ids={['x', 'y']} />);
  const counts = () => [
    document.querySelectorAll('.a').length,
    document.querySelectorAll('.b').length,
  ];
  view.rerender(<Two ids={[]} />);
  expect(counts()).toEqual([0, 0]); // React: [0, 0]. Redact: [2, 0]
  view.rerender(<Two ids={['x', 'y']} />);
  expect(counts()).toEqual([2, 2]); // React: [2, 2]. Redact: [4, 2]
});

React 19.3.1 passes. Redact 0.3.1 fails: after emptying, the first list's two nodes are still in the DOM ([2, 0]), and after refilling there are four ([4, 2]). In a larger case (30 items, then a grouped list with some single-item groups), the nodes whose keys collided were the ones left behind.

Related

This is likely the shared key space that #149 mentions as a side effect of flattening nested arrays into the parent's child list. The candidate fix described there, which gives each nested array its own slot and key scope, may cover it. This repro could be added as a regression case for orphaned nodes, which #149's own repro does not show.

主要言語
TypeScript
スター
640
フォーク
32
平均マージ
1時間 15分
マージ済み PR(30日)
7

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/redact のほかの issue

TanStack/redact の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。