reactuals
Version:
A useful package providing a collection of 50+ React hooks and utilities to simplify React development.
15 lines (14 loc) • 492 B
JavaScript
import { useEffect, useState } from "react";
/**
* Returns the current scroll Y position of the window.
* Automatically updates on scroll.
*/
export function useScrollPosition() {
const [scrollY, setScrollY] = useState(window.scrollY);
useEffect(() => {
const handleScroll = () => setScrollY(window.scrollY);
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
return scrollY;
}