Download react-use/src/useIntersection.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 913 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useIntersection.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useIntersection.ts
-
curl -L -o useIntersection.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useIntersection.ts
913 Bytes
| import { RefObject, useEffect, useState } from 'react'; | |
| const useIntersection = ( | |
| ref: RefObject<HTMLElement>, | |
| options: IntersectionObserverInit | |
| ): IntersectionObserverEntry | null => { | |
| const [intersectionObserverEntry, setIntersectionObserverEntry] = | |
| useState<IntersectionObserverEntry | null>(null); | |
| useEffect(() => { | |
| if (ref.current && typeof IntersectionObserver === 'function') { | |
| const handler = (entries: IntersectionObserverEntry[]) => { | |
| setIntersectionObserverEntry(entries[0]); | |
| }; | |
| const observer = new IntersectionObserver(handler, options); | |
| observer.observe(ref.current); | |
| return () => { | |
| setIntersectionObserverEntry(null); | |
| observer.disconnect(); | |
| }; | |
| } | |
| return () => {}; | |
| }, [ref.current, options.threshold, options.root, options.rootMargin]); | |
| return intersectionObserverEntry; | |
| }; | |
| export default useIntersection; | |