Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
React: Is it okay to use React.Children.toArray to fix each child in a list should have a unique "key" prop

In Reactjs is it okay to use React.Children.toArray method to fix the warning:

Warning: Each child in a list should have a unique "key" prop.

For example:

React.Children.toArray(myArray.map(value => <div>{value}</div>));

the code above seems to fix the warning but I wonder if it is a good practice

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

No, that's not good practice. The reason react gives a warning that keys are needed is that react needs the keys to tell which element corresponds to which between renders. Your code won't help react to achieve that, so you're just covering up the problem, and not fixing it.

Ideally, if your array of data contains unique ids, you should use those as the keys:

hypotheticalArray.map(data => <div key={data.id}>{data.value}</div>);

If there aren't unique ids, then index can be used as a last resort, but it will misbehave if you change the order of the array.

myArray.map((value, index) => <div key={index}>{value}></div>);
about 4 years ago · Juan Pablo Isaza Denunciar

0

use this instead .

myArray.map((value,index) => <div key={index}  >{value}</div>)

key in react element(child) is added inorder to keep uniqueness of the children, keys are generally used in a list of childrens. If any of the child element changes then key prop helps react which children did got changed as it would have a unique key prop .

about 4 years ago · Juan Pablo Isaza Denunciar

0

This way you pass the key as a unique value

React.Children.toArray(myArray.map(value => <div key={value.id}>{value}</div>));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda