kx3dex_radio/hooks/use-scroll.tsx

21 lines
528 B
TypeScript
Raw Permalink Normal View History

2024-04-11 02:40:31 +00:00
import { useCallback, useEffect, useState } from 'react';
export default function useScroll(threshold: number) {
const [scrolled, setScrolled] = useState(false);
const onScroll = useCallback(() => {
setScrolled(window.scrollY > threshold);
}, [threshold]);
useEffect(() => {
window.addEventListener('scroll', onScroll);
return () => window.removeEventListener('scroll', onScroll);
}, [onScroll]);
// also check on first load
useEffect(() => {
onScroll();
}, [onScroll]);
return scrolled;
}