MCPcopy Create free account
hub / github.com/github/docs / useWindowScroll

Function useWindowScroll

components/hooks/useWindowScroll.ts:4–24  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2
3// returns scroll position
4export function useWindowScroll(): number {
5 const [scrollPosition, setScrollPosition] = useState(
6 typeof window !== 'undefined' ? window.scrollY : 0
7 )
8
9 useEffect(() => {
10 const setScollPositionCallback = () => setScrollPosition(window.scrollY)
11
12 if (typeof window !== 'undefined') {
13 window.addEventListener('scroll', setScollPositionCallback)
14 }
15
16 return () => {
17 if (typeof window !== 'undefined') {
18 window.removeEventListener('scroll', setScollPositionCallback)
19 }
20 }
21 }, [])
22
23 return scrollPosition
24}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected