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

129
Visualizações
Event.target.id returns empty string

Hello I have a problem with returning an id value (defined in the key attribute) of the image when using event.target.

function imageClicked(event) {
    console.log(event.target.key);
    setClicked(event.target.key);
    shuffle(GameData);
    if (clicked.includes(event.target.key) === false) {
      setScore(score + 1);
      setClicked([...clicked, event.target.key]);
    } else {
      setScore(0);
      setClicked([]);
    }
  }

...

 <Display onClick={imageClicked} items={GameData} />

And component fragment:

<div className="display">
      <img
        src={props.image}
        alt={props.name}
        key={props.id}
        onClick={props.onClick}
      />
      <p>{props.name}</p>
    </div>

However when I am using an alt attribute of the image the app works as intended. But I want to change it anyway as I creacted ids for all images and they are currently unused.

Thanks in advance.

Here is the link to the full app (using alt attribute) in CodeSandbox.

about 4 years ago · Juan Pablo Isaza
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