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

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