Created
June 27, 2019 11:49
-
-
Save steelydylan/e595c2f5f0aad5b9e79efc400e63438c to your computer and use it in GitHub Desktop.
Windowのスクロール状態を監視するためのReact hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
スクロールが開始されたらtrue, スクロールが終わったタイミングでfalseを返すhook