@gitgw/use-scroll
Version:
React Hook to get X/Y coordinates of current position of the scroll.
23 lines (17 loc) • 448 B
JavaScript
import { useEffect, useState } from "react";
export const useScroll = () => {
const [state, setState] = useState({
x: 0,
y: 0,
});
const handleScroll = () => {
setState({ x: window.scrollX, y: window.scrollY });
};
useEffect(() => {
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);
return state;
};