Skip to content

Instantly share code, notes, and snippets.

@marcovincit
Created July 14, 2021 23:00
Show Gist options
  • Save marcovincit/b3408079769f317bd05182b745db0e20 to your computer and use it in GitHub Desktop.
Save marcovincit/b3408079769f317bd05182b745db0e20 to your computer and use it in GitHub Desktop.
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