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

129
Views
active class in react js

sorry to ask. I have a button, then I add an active class when I click it, but when I click it all the buttons become active, how do I fix it? this is the code

  <div className="">
          {category.items.length === 0 ? (
            <div>There is no property at this categories</div>
          ) : (
            category.items.map((item, index2) => {
              return (
                <div key={`category-${index1}-item-${index2}`}>
                  {buttons.map((buttonLabel, i) => (
                    <button
                      key={i}
                      name={buttonLabel}
                      onClick={(event) => handleClick(event, i)}
                      className={
                        i === clickedId
                          ? 'py-4 pl-3 customButton active '
                          : 'customButton py-4 pl-3'
                      }
                    >
                      {item.name}
                    </button>
                  ))}
                </div>
              );
            })
          )}
        </div>

and this is code when button called

<ButtonCategory
          data={category.categories}
          buttons={[category.categories]}
          doSomethingAfterClick={printButtonLabel}
        />

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you are returning the same set of buttons for each item. there are better ways to do this but with you current setup this should work:

  buttons.map((buttonLabel, i) => {
    const key = `${index2}.${i}`
    return (
    <button
      key={key}
      name={buttonLabel}
      onClick={(event) => handleClick(event, key)}
      className={
        key === clickedId
          ? "py-4 pl-3 customButton active "
          : "customButton py-4 pl-3"
      }
    >
      {item.name}
    </button>
  )});

just don't forget to initialize your clickedId state as null or undefned instead of number

about 4 years ago · Juan Pablo Isaza Report

0

youd should set index of the clicked button as clickedId in handleClick function. so your handleClick function must be like this

const handleClick = (event, i) => {
    setClickedId(i)
    // other logic
}
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!