Unkeyed siblings after a .map() list are remounted when the list changes length (React keeps them)
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 48/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Activo
- Stack tecnológico
- react, typescript
- Área
- frontend
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- TypeScript
- Estrellas
- 581
- Forks
- 28
- Merge medio
- 13 h 48 min
- PR fusionados (30 d)
- 45
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de TanStack/redact
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
TanStack/redact#142 · 4 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Dificultad 4/5 1-2 días Aptitud para principiantes 35/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 20/100
Los mantenedores suelen responder en 1 día
Todos los issues de TanStack/redact
Issues similares
-
inference/go type/feature
Dificultad 1/5 1-3 horas Aptitud para principiantes 72/100
anomalyco/opencode#65148 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
truapi-release
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
paritytech/product-sdk#529 ·
Los mantenedores suelen responder en 1 día
-
Add: Mansudae TVAbiertoapproved check:passed streams:add
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
Los mantenedores suelen responder en 1 día
-
[Perf] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per nodeAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
devlikeapro/waha#2369 ·
Los mantenedores suelen responder en 5 días