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

109
Visualizações
How to implement debounce inside of useEffect when dealing with Event Listeners?

I see questions aplenty about useEffect and debounce but this use case is a little different. I was having some trouble with fetchBookList not having access to state variables in the same component and it turns out on mount that function only has access to "stale" state, hence this:

useEffect(() => {
  const handleScroll = () => {
    if (Math.ceil(window.innerHeight + window.scrollY) >= document.documentElement.scrollHeight) {
      fetchBookList();
    }
  }
  window.addEventListener('scroll', handleScroll, {
    passive: true
  });
  return () => {
    window.addEventListener('scroll', handleScroll);
  }

}, [bookList, bookType])

Re-creating the handleScroll is not the optimal solution but it does work. The problem is, with this code when the user hits the bottom of the screen there's no throttling in place so instead of fetching just once, multiple calls go out as in an instant the viewport may or may not be at the very bottom of the screen.

In this particular case, how does one go about debouncing the fetchBookList call so it only fires once every second or so max?

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