Created
August 19, 2022 01:48
-
-
Save azaek/ff8c4d061d931dbcef2173e66fe87700 to your computer and use it in GitHub Desktop.
useScroll
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
import { useEffect, useState } from "react"; | |
const useScroll = () => { | |
const [scrollPosition, setScrollPosition] = useState(0); | |
useEffect(() => { | |
const updatePosition = () => { | |
setScrollPosition(window.pageYOffset); | |
} | |
window.addEventListener("scroll", updatePosition); | |
updatePosition(); | |
return () => window.removeEventListener("scroll", updatePosition); | |
}, []); | |
return scrollPosition; | |
} | |
export default useScroll; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment