reactjs / reactjs/react.dev

It is a myth and misunderstanding the key={...} make the display correct

Abierto
#6,292 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
JavaScript
Estrellas
11.8k
Forks
7.9k
Merge medio
1 d 11 h
PR fusionados (30 d)
11

Descripción

On page https://react.dev/learn/rendering-lists

it is said that:

A well-chosen key helps React infer what exactly has happened, and make the correct updates to the DOM tree.

That's a myth that the key keeps the updates correct. It helps to make the update faster, so that it goes by row level diff'ing, instead of cell level diff'ing.

For example, if you have a table with 5 fields. They key can make it so that the diff'ing happens with the prev row, so that it is faster.

Imagine if row 23 supposedly needed to compare with row 24, due to row added or deleted, but due to the key being bad, it is compared to row 23, and all the fields are different, so React needs to update it cell by cell.

If the key is good, however, now row 23 will be compared with row 24, and nothing changes, and no update is needed whatsoever.

You can check with an advanced engineer about this. The myth about "you need a good key so that the displayed data is correct", is wrong.

Maybe the original line "make the correct updates to the DOM tree" doesn't suggest that, but it can make readers think this way. So you may want to rephrase it or further explain it.

So this part later in the page:

You might be tempted to use an item’s index in the array as its key. In fact, that’s what React will use if you don’t specify a key at all. But the order in which you render items will change over time if an item is inserted, deleted, or if the array gets reordered. Index as a key often leads to subtle and confusing bugs.

is also a wrong concept. It will NOT lead to subtle and confusing bugs. It will just make it slow, but the display will still all be correct.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza leyendo la página Rendering Lists en https://react.dev/learn/rendering-lists, incluidas las secciones citadas sobre las claves y los índices de arrays. Verifica las afirmaciones frente al comportamiento documentado de React y determina si el texto necesita una aclaración; se considera terminado cuando en esa página se refleja una explicación consensuada y técnicamente correcta.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, react
Área
documentation
Tipo de issue
Documentación
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.