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

200
Vistas
why class not render after set state in react js?

I want to add an active class to the indicator after clicking it but the active class doesn't render even though list.id === active

const [active, setActive] =  useState(1);

const scrollToElement = (id, value) => {
    const element = document.getElementById(`${value}`);
    element.scrollIntoView({ behavior: 'smooth' });

    setActive(prevState => {
      return {
        ...prevState,
        active: id
      }
    });
  }
  
<div
  key={list.id}
  className={`${list.id === active ? `${style.active} ${style.dot}` : `${style.dot}`}`}
  onClick={() => scrollToElement(list.id, list.name)}
/>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are returning an object for a new state value.

Try this:

const [active, setActive] = useState(1);

const scrollToElement = (id, value) => {
    const element = document.getElementById(`${value}`);
    element.scrollIntoView({ behavior: 'smooth' });

    setActive(id);
}

<div
    key={list.id}
    className={`${list.id === active ? `${style.active} ${style.dot}` : `${style.dot}`}`}
    onClick={() => scrollToElement(list.id, list.name)}
/>
about 4 years ago · Juan Pablo Isaza Denunciar
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