Created
July 14, 2021 23:00
-
-
Save marcovincit/b3408079769f317bd05182b745db0e20 to your computer and use it in GitHub Desktop.
This file contains hidden or 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"; | |
export default function useWindowScroll() { | |
const [windowScrollX, setWindowScrollX] = useState(0); | |
const [windowScrollY, setWindowScrollY] = useState(0); | |
useEffect(() => { | |
function updateWindowScroll() { | |
setWindowScrollX(window.scrollX); | |
setWindowScrollY(window.scrollY); | |
} | |
window.addEventListener("scroll", updateWindowScroll); | |
updateWindowScroll(); | |
return () => window.removeEventListener("scroll", updateWindowScroll); | |
}, []); | |
return { windowScrollX, windowScrollY }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment