# useViewportSpy A hook that uses the [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver) API to tell whether the provided DOM Element is visible within the viewport. ### Why? 💡 - Asynchronously observes changes in the intersection of the given element with the document - Can be used to perform animation when the component appear into the viewport ### Basic Usage: ```jsx harmony import { useRef } from 'react'; import { Space, Typography, Tag } from 'antd'; import useViewportSpy from 'beautiful-react-hooks/useViewportSpy'; const ViewportSpyComponent = () => { const ref = useRef(); const isVisible = useViewportSpy(ref); return (
Observed element
is the observed element in viewport? {isVisible ? 'yes' : 'no'}
); }; ``` ### Options Pass [IntersectionObserver options](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver) as second parameter to customize the behavior of the hook ```jsx harmony import { useRef } from 'react'; import { Space, Typography, Tag } from 'antd'; import useViewportSpy from 'beautiful-react-hooks/useViewportSpy'; const ViewportSpyComponent = () => { const ref = useRef(); const isVisible = useViewportSpy(ref, { threshold: 0.6 }); return (
Observed element
is the observed element in viewport? {isVisible ? 'yes' : 'no'}
); }; ``` #### ✅ Pro tip: This hook can be used to perform animations when a component/element enters or exit the viewport. To deeply understand [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver) please read [Using the Intersection Observer API to Trigger Animations and Transitions](https://alligator.io/js/intersection-observer) ### Types ```typescript static import { type RefObject } from 'react'; /** * Uses the IntersectionObserverMock API to tell whether the given DOM Element (from useRef) is visible within the * viewport. */ declare const useViewportSpy: (ref: RefObject, options?: IntersectionObserverInit) => boolean | undefined; export default useViewportSpy; ```