Skip to content

Instantly share code, notes, and snippets.

@azaek
Created August 19, 2022 01:48
Show Gist options
  • Save azaek/ff8c4d061d931dbcef2173e66fe87700 to your computer and use it in GitHub Desktop.
Save azaek/ff8c4d061d931dbcef2173e66fe87700 to your computer and use it in GitHub Desktop.
useScroll
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