Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!