UNPKG

@j-hooks/use-scroll

Version:

React Hook to get X/Y coordinates of current position of the scroll.

17 lines (15 loc) 395 B
import { useState, useEffect } from "react"; export const useScroll = () => { const [state, setState] = useState({ x: 0, y: 0, }); const onScroll = () => { setState({ y: window.scrollY, x: window.scrollX }); }; useEffect(() => { window.addEventListener("scroll", onScroll); return () => window.removeEventListener("scroll", onScroll); }, []); return state; };