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

62
Views
Adding a Class and Setting States Within The Same Function In React

I have a function in React in which I'm trying to change the state and add a class that styles the clicked button. The problem is that when I avoid altering the state in the userAnswers() function the style does work, but when I add setChoices() the style doesn't work. This is the code:

function userAnswers (e) {
    
    const choiceBtns = document.querySelectorAll(".choice-btns")
    choiceBtns.forEach((btn) => {
        if (e.target.id === btn.id) {
            btn.classList.remove("chosen")
        }
    })
    e.target.classList.add("chosen")
    
    setChoices((prev) => {
        return prev.map((q) => {
            return {...q, [e.target.value]: false}
        })
    })
    
    setChoices((prev) => {
        return prev.map((q) => {
            if (q.id === e.target.id) {
                return {...q, [e.target.value]: true}
            } else {
                return {...q}
            }
        })
    })
    
}

I don't know I'm missing something here! Any help would be appreciated.

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