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

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

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

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

評価

難易度
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分
マージ済み PR(30日)
45

環境構築

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

はじめの一歩

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

TanStack/redact のほかの issue

TanStack/redact の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

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

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