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

Ouverte
#192 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
58/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
react, typescript
Domaine
frontend, testing

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
TypeScript
Étoiles
640
Forks
32
Merge moyen
1 h 15 min
PR mergées (30 j)
7

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de TanStack/redact

Toutes les issues de TanStack/redact

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.