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

141
Visualizações
How do I pass an object property to an event handler function in order to perform DOM manipulation?

I have an HTML page rendered using React. I've retrieved a list of objects representing websites and stored them in memory, and can display them in a long list on the page just fine.

I've added a button for each object rendered on the page. I've created an event handler for the onClick that should receive a property from the object (uid), then generate a new table on the page with details specific to that object.

I think I'm missing something crucial in React, because I can't seem to pass the object data to the event handler.

HTML/JSX

   <div>
        <h2>results</h2>
        <ul>
          {countriesToShow.map((x, i) => {
            let uid = x.uid;
            return (
              <li key={i} id={i}>
                {x.name.common}{' '}
                <button onClick={toggleShowCountry(uid)}>show</button>
              </li>
            );
          })}
        </ul>
      </div>
    )

toggleShowCountry

const toggleShowCountry = (x) => {
    console.log('this is uid', x);
    // TODO: get DOM object where the country resides and manipulate it
  };

Output

Nothing. Not even undefined. Clicking the button doesn't cause anything to appear in the console, and on the initial render it looks like toggleShowCountry fires automatically for each object as it is rendered.

I believe this might have somethign to do with the "bind" feature of JavaScript but am not totally sure.

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

0

try this one: <button onClick={() => toggleShowCountry(uid)}>show</button>

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