Skip to content

Instantly share code, notes, and snippets.

@steelydylan
Created June 27, 2019 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steelydylan/e595c2f5f0aad5b9e79efc400e63438c to your computer and use it in GitHub Desktop.
Save steelydylan/e595c2f5f0aad5b9e79efc400e63438c to your computer and use it in GitHub Desktop.
Windowのスクロール状態を監視するためのReact hooks
export const useScrolling = (delay: number) => {
const [scrolling, setScrolling] = useState(false);
useEffect(() => {
let debounceTimer = null;
const eventHandler = () => {
if (scrolling === false) {
setScrolling(true);
}
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
setScrolling(false);
}, delay);
}
window.addEventListener('scroll', eventHandler);
return (() => {
window.removeEventListener('scroll', eventHandler);
})
}, []);
return scrolling;
}
@steelydylan
Copy link
Author

スクロールが開始されたらtrue, スクロールが終わったタイミングでfalseを返すhook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment