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

277
Visualizações
React "Warning: Encountered two children with the same key"

I have a React app that mapping cards and each card have unique id although I'm getting error in console that some are not unique:

Warning: Encountered two children with the same key, 2294264. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

Here is the code for building my card structure:

function CreateCards(doc) {
  return (
    <SimpleCard
      key={doc.newsid}
      theCardId={doc.newsid}
      categorietitle={doc.categorietitle}
      newstitle={doc.newstitle}
      date={format(new Date(doc.date), "dd/MM/yyyy")}
      thenews={doc.thenews}
      newsurl={doc.newsurl}
    />
  );
}

And here is the code for mapping the cards:

      <div className="general-card1">
        {this.state.noPlaceFound ? (
          <h3 className="noPlaceFound">
            <i className="fas fa-exclamation-circle fa-sm WarnIcon"></i>
            لا يوجد نتائج
          </h3>
        ) : (
          this.state.WasMap.map((v) => CreateCards(v._source))
        )}
      </div>

Can you please help me?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is because when you render a list of components in a map(), each component should have a unique key property. It is for React to distinct them. What you did wrong is that you added the key inside the component, where it should be when rendering inside map().

Notice that key = {index}. This would work for most cases, but it's better to use a custom id (you could replace index with v._source.newsid if this is an unique field).

<div className="general-card1">
  {this.state.noPlaceFound ? (
    <h3 className="noPlaceFound">
      <i className="fas fa-exclamation-circle fa-sm WarnIcon"></i> لا يوجد نتائج
    </h3>
  ) : (
    this.state.WasMap.map((v, index) => <CreateCards doc={v._source} key={index} />)
  )}
</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