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

108
Views
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
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!