Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda