Unkeyed siblings after a .map() list are remounted when the list changes length (React keeps them)
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 48/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- react, typescript
- Bereich
- frontend
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 581
- Forks
- 28
- Ø Merge
- 13 Std. 48 Min.
- Gemergte PRs (30 T.)
- 45
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus TanStack/redact
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
TanStack/redact#142 · 4 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 1-2 Tage Anfängerfreundlichkeit 35/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 20/100
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in TanStack/redact
Ähnliche Issues
-
inference/go type/feature
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 72/100
anomalyco/opencode#65148 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
truapi-release
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
paritytech/product-sdk#529 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Add: Mansudae TVOffenapproved check:passed streams:add
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
Maintainer antworten meist innerhalb von 1 Tag
-
[Perf] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per nodeOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 77/100
devlikeapro/waha#2369 ·
Maintainer antworten meist innerhalb von 5 Tagen