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

246
Vistas
Is it OK to use the "=" when trying to declare a body class?

So I am using State to change my body's background color using classes. Basically there's a scroll effect where it starts off in "dark mode" and becomes "light mode" after hitting a specific ref. Here's my code snippet:

const shiftRef = useRef(null)
const [ changeMode, setChangeMode ] = useState("dark-mode");
    const handleChangeMode = () => {
        if (shiftRef.current) {
            let main = shiftRef.current.getBoundingClientRect();
            if (main.top <= 160) {
                setChangeMode("light-mode")
            } else {
                setChangeMode("dark-mode")
            }
            console.log(main.top)
        }
    }
    useEffect(() => {
        document.body.classList = changeMode;
        window.addEventListener("scroll", handleChangeMode);
        return () => window.removeEventListener("scroll", handleChangeMode);
    }, [changeMode]);

Now I have it changing the body instead of the component container so that the color is always there in the background -- if i didn't do that, the color would "clip" if I scrolled beyond the page's parameters, which would not give the effect I need.

Here's my question...is it valid to declare my body's class in the Effect hook with document.body.classList = {add class here}? The only reason I ask is usually that has an additional action like .add or .remove or .contains or something but I'm not doing that here. Also: I should point out everything compiled successfully, and there's no warnings/alerts in my console...so I guess it's ok? But I figured I'd ask people here since you all give a lot of great info.

Thank you!

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