Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!