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

Function useOnScreen

components/hooks/useOnScreen.ts:3–24  ·  view source on GitHub ↗
(
  ref: MutableRefObject<T | undefined> | RefObject<T>,
  options?: IntersectionObserverInit
)

Source from the content-addressed store, hash-verified

1import { useState, useEffect, MutableRefObject, RefObject } from 'react'
2
3export function useOnScreen<T extends Element>(
4 ref: MutableRefObject<T | undefined> | RefObject<T>,
5 options?: IntersectionObserverInit
6): boolean {
7 const [isIntersecting, setIntersecting] = useState(false)
8 useEffect(() => {
9 let isMounted = true
10 const observer = new IntersectionObserver(([entry]) => {
11 isMounted && setIntersecting(entry.isIntersecting)
12 }, options)
13
14 if (ref.current) {
15 observer.observe(ref.current)
16 }
17
18 return () => {
19 isMounted = false
20 ref.current && observer.unobserve(ref.current)
21 }
22 }, [Object.values(options || {}).join(',')])
23 return isIntersecting
24}

Callers 2

GHAEReleaseNotePatchFunction · 0.90
GHESReleaseNotePatchFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected