reactjs / reactjs/react.dev

improve the phrasing for using "no keys" on https://reactjs.org/docs/lists-and-keys.html#keys

Abierto
#3,732 1 comentario 0 reacciones 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

https://reactjs.org/docs/lists-and-keys.html#keys currently has a section that causes confusion about React's behaviour when omitting key attributes, currently reading:

"We don’t recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny’s article for an in-depth explanation on the negative impacts of using an index as a key. If you choose not to assign an explicit key to list items then React will default to using indexes as keys."

The emphasized sentence has led to folks thinking that React knows what a specific map()'s iteration index is, and that it'll use that, rather than that the index talked about is the child index of the element in the vdom. To remove that confusion and make it explicit that React is using an internal index, I would recommend changing the phrasing here to:

"We don’t recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny’s article for an in-depth explanation on the negative impacts of using an index as a key. If you choose not to assign an explicit key to list items then React will default to using element index in the virtual DOM tree as keys."

It might be even more high value to also add one or more sentences to explain that this means that the behaviour for consecutive maps that don't use keys vs. incorrectly use iteration indices as keys will lead to different behaviour, which folks right now mistakenly assume would behave the same.

E.g.

render() {
  const { text, additional } = this.state;
  const content = text.map(t => <p>{t}</p>);
  const addenda = additional.map(t => <p className="add">{t}</p>);
  return <section>{content}{addenda}</section>;
}

will work, but:

render() {
  const { text, additional } = this.state;
  const content = text.map((t,i) => <p>{t}</p>);
  const addenda = additional.map((t,i) => <p className="add" key={i}>{t}</p>);
  return <section>{content}{addenda}</section>;
}

will not, and if you are familiar enough with React already to not need the docs, you know why, but if you're not and you read the docs, it is entirely understandable if you come away convinced that this should work after reading the current text.

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

Comienza con la sección Keys en https://reactjs.org/docs/lists-and-keys.html#keys y revisa la redacción sobre las keys omitidas junto con los ejemplos proporcionados. Aclara que React utiliza el índice del elemento en el virtual DOM tree y decide si también debería añadirse el ejemplo de map consecutivos; la documentación debe distinguir ambos comportamientos con precisión.

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
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.