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

119
Views
useEffect triggers everything

Hi just trying to learn about hooks inside a Next.JS project. Have implemented a useEffect to listen for scrolling behavior with the goal of displaying different content in a header is the page is scrolled.

const [ scrollY, setScrollY ] = useState(0);

useEffect(() => {
        const handleScroll = () => {
            setScrollY(window.scrollY);
        };
        handleScroll();
        
        window.addEventListener("scroll", handleScroll);
        return () => {
            window.removeEventListener("scroll", handleScroll);
        };
    }, []);

Then i just have something like

<div className={scrollY > 20 ? 'scrolled': null}>

This works for its purpose but an unintended effect is that scrolling is now additionally triggering everything in the component, so for example if I have console.logs outwith the useEffect they would get triggered on every scroll of the page. Is it possible to have my UE purely update the scrollY state?

Example of an issue would be:

const [ scrollY, setScrollY ] = useState(0);
const test = "test";
console.log(test);

useEffect(() => {
        const handleScroll = () => {
            setScrollY(window.scrollY);
        };
        handleScroll();
        
        window.addEventListener("scroll", handleScroll);
        return () => {
            window.removeEventListener("scroll", handleScroll);
        };
    }, []);

Test would be logged repeatedly on every scroll.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It will trigger many times,because every time you scroll,the function component would be called,and the view would update.

about 4 years ago · Juan Pablo Isaza Report

0

You use State in your Component. When you call the setScrollY(), you are updating the State, which triggers a re-render of your component.

If you want to use console.log to log the value of the scrollY, you could do it when you set scrollY instead of on each run. You should also ask yourself if scrollY needs to be stored as State, and if so, why.

about 4 years ago · Juan Pablo Isaza Report

0

I'd suggest putting the header logic inside the scroll handler.

Like so:

const [ isScrolled, setIsScrolled ] = useState(false);

...
const handleScroll = () => {
    if (window.scrollY > 20 && !isScrolled) {
        setIsScrolled(true);
    }

    if (window.scrollY <= 20 && isScrolled) {
        setIsScrolled(false);
    }
};
...

<MyComponent attribute={isScrolled && 'scrolled'} />

This will drastically reduce the amount of rerenders by setting the state whenever the scroll position meets the required point.

about 4 years ago · Juan Pablo Isaza Report
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!