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

79
Visualizações
React Bug In Displaying Element With The Same Key

I accidentally wrote some bad codes today and I would like to know why React shows me that result.

My code is something like this:

class Prova extends React.Component {
  
  constructor (props) {
    super(props);
    this.state = {
      people : [],
      counter : 0,
      person : {
        name: "Michele",
        surname: "Scavino"
      } 
   }
}
  
  addPeople = () => {
    this.state.person["id"] = this.state.counter;
    this.state.people = [
      ...this.state.people,
      this.state.person
    ];

    this.setState({counter: this.state.counter + 1});
  }

  render() {     
    return (
      <div>
        {
          this.state.people?.map(el => {
            return (
              <div key={el.id}>
                {el.name}
                {el.surname}
                {el.id}
              </div>
            )
          })
        }    
       <br />
       <br />
       <br />
       <br />
       <button onClick={this.addPeople}>Add person</button>
      </div>
    );
  }
}

  ReactDOM.render(
    <Prova />,
    document.getElementById('root')
  );

The error here is that whenever I add a new person with the apposite button I copy the same ref of the same object in the array and when I go to change the ID of an element of the array, all of the ID of the elements of the array will change, using the spread operatore in this way. But the problem here is another. Whenever I add a new person also the displaying of new elements is very strange and I can't undestand why. If I click the button six times it should display to me 6 rows but actually it doesn't happen. It litterally freakout!!! I know that I'm trying to display a list of elements with the same keys but I can't realize why this trouble happen... Is there any expert who can answer my question appropiately or link me some resources to find out the answer to my question? I really suggest you to copy and run my code to see the bug with you eyes. Thank you!

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

0

You can't use an incremental id from within your component since it will be changed along with every render, Reactjs still miss your last rendering Id.

You will need to have a consistent id during renders:

One very popular solution is to use this UUID generator: https://www.npmjs.com/package/uuid

about 4 years ago · Juan Pablo Isaza Relatório

0

key in a map must be unique. You can use id to set a key:

{
  this.state.people?.map((el) => {
    return (
      <div key={el.id}>
        {el.name}
        {el.surname}
        {el.id}
      </div>
    );
  });
}

And update addPeople like this:

addPeople = () => {
  this.state.people = [...this.state.people, { ...this.state.person, id: this.state.counter }];

  this.setState({ counter: this.state.counter + 1 });
};
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