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

126
Vistas
React keys for an array of strings

I have a question. How can I generate key for array of string? It is not object, in object I could put id and it would solve the problem. So what should I do in just array? Indexes as key is not a good practice so I am stuck.

const ingredients = ['rice', 'curry', 'chicken]



    {props.ingredients.map((ingredient) => (
                <Box>{ingredient}</Box>
          ))}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If you don't have any duplicate ingredients, using the ingredient name as the key would be fine.

{props.ingredients.map((ingredient) => (
    <Box key={ingredient}>{ingredient}</Box>
))}

If you will have duplicate ingredients, and you really care about getting the key right, you could use the index if the index won't change:

{props.ingredients.map((ingredient, i) => (
    <Box key={i}>{ingredient}</Box>
))}

(Using the index isn't universal bad practice, it's just only suitable for some situations)

If you could have duplicate ingredients and the index can change, and you really want to fix this, change it to an array of objects instead of an array of strings, with a uniquely identifying attribute on each object that you can use as a key.

about 4 years ago · Juan Pablo Isaza Denunciar

0

const ingredients = ['rice', 'curry', 'chicken']
 
{props.ingredients.map((ingredient, index) => ( 
   <Box key={index}>{ingredient}</Box>
 ))}
about 4 years ago · Juan Pablo Isaza Denunciar

0

{props.ingredients.map((ingredient, index) => (<Box key={index}>{ingredient}</Box>))}

You get an index built in when doing it like this and can use it as they key.

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