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

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
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