HooksForUI
Scrolldetector

Scroll Detector

This Scroll Detector is used for triggering the infinity scroll for the lists.

"PS: That is not a Hook"

Scroll Detector

scrollDetector.tsx
import React, { useEffect } from 'react'
 
interface IScrollDetector {
  onScrollEnd: Function
  children: React.ReactNode
}
 
export const ScrollDecter = ({ onScrollEnd, children }: IScrollDetector) => {
  let prevScrollTop: number = 0
 
  function handleScroll() {
    const { scrollTop, scrollHeight, clientHeight } = document.documentElement
 
    if (scrollTop > prevScrollTop) {
      if (scrollTop + clientHeight >= scrollHeight - 700) {
        onScrollEnd()
      }
    }
    prevScrollTop = scrollTop
  }
 
  useEffect(() => {
    window.addEventListener('scroll', handleScroll)
    return () => {
      window.removeEventListener('scroll', handleScroll)
    }
  }, [])
 
  return <div>{children}</div>
}

Usage

products.tsx
<ScrollDetector onScrollEnd={fetchFunction}>
    <></>
</ScrollDector>
Last updated on April 25, 2023