Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

63
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda