Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda