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

64
Visualizações
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
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