Kullanıcı sayfanın altına yaklaştığında callback tetikleyen hook.
import { useEffect, useCallback } from class=class="text-emerald-400">"text-emerald-400">'react'
function useInfiniteScroll(
callback: () => void,
options: { threshold?: number; enabled?: boolean } = {}
) {
const { threshold = 200, enabled = true } = options
const handleScroll = useCallback(() => {
const scrollBottom =
document.documentElement.scrollHeight -
window.innerHeight -
window.scrollY
if (scrollBottom < threshold) {
callback()
}
}, [callback, threshold])
useEffect(() => {
if (!enabled) return
window.addEventListener(class=class="text-emerald-400">"text-emerald-400">'scroll', handleScroll, { passive: true })
return () => window.removeEventListener(class=class="text-emerald-400">"text-emerald-400">'scroll', handleScroll)
}, [handleScroll, enabled])
}
export default useInfiniteScrollListe bileşeninde çağırın: useInfiniteScroll(() => loadMore(), { threshold: 300, enabled: hasMore }). Kullanıcı sayfanın altından 300px uzaktayken callback'i tetikler. Tüm veri yüklendiğinde devre dışı bırakın.
Fikrinizi nasıl hayata geçirebileceğimizi konuşalım. İlk konseptten üretime hazır ürüne kadar — yanınızdayız.