Gathering detailed insights and metrics for react-intersection-observer-hook
Gathering detailed insights and metrics for react-intersection-observer-hook
Gathering detailed insights and metrics for react-intersection-observer-hook
Gathering detailed insights and metrics for react-intersection-observer-hook
react-intersection-observer
Monitor if a component is inside the viewport, using IntersectionObserver API
@react-hook/intersection-observer
A React hook for the IntersectionObserver API that uses a polyfill when the native API is not available
@shopify/react-intersection-observer
A React wrapper around the Intersection Observer API
@researchgate/react-intersection-observer
React component for the Intersection Observer API
React hook to use IntersectionObserver declaratively.
npm install react-intersection-observer-hook
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
73 Stars
58 Commits
7 Forks
1 Watching
1 Branches
1 Contributors
Updated on 20 Nov 2024
Minified
Minified + Gzipped
TypeScript (71.19%)
JavaScript (28.46%)
CSS (0.35%)
Cumulative downloads
Total Downloads
Last day
-0.9%
19,972
Compared to previous day
Last week
2.5%
106,439
Compared to previous week
Last month
4.7%
449,726
Compared to previous month
Last year
34.7%
4,632,562
Compared to previous year
This is a simple to use React hook package for using Insersection Observer declaratively. By using this hook, you can easily track if a component is visible or not, create lazy loading images, trigger animations on entering or leaving the viewport, implement infinite loading etc.
Live demo is here.
This package relies on Intersection Observer API. Browser compatibility can be seen in here.
If you want to support the browsers those are not supporting it natively, you can use a polyfill.
1npm install react-intersection-observer-hook
1import React, { useEffect } from 'react'; 2import { useIntersectionObserver } from 'react-intersection-observer-hook'; 3// ... 4 5function Example() { 6 // `useIntersectionObserver` returns a tuple. 7 // We need to give this `ref` callback to the node we want to observe. 8 // The second item, `entry` is the response of the `IntersectionObserver` instance. 9 const [ref, { entry }] = useIntersectionObserver(); 10 const isVisible = entry && entry.isIntersecting; 11 12 useEffect(() => { 13 console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`); 14 }, [isVisible]); 15 16 return <SomeComponentToTrack ref={ref} />; 17}
If you have a scrollable container, you can set a root
like this:
1import React, { useEffect } from 'react'; 2import { useIntersectionObserver } from 'react-intersection-observer-hook'; 3// ... 4 5function Example() { 6 const [ref, { entry, rootRef }] = useIntersectionObserver(); 7 const isVisible = entry && entry.isIntersecting; 8 9 useEffect(() => { 10 console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`); 11 }, [isVisible]); 12 13 return ( 14 <ScrollableContainer 15 // We use `rootRef` callback to set the root node. 16 ref={rootRef} 17 > 18 <SomeComponentToTrack ref={ref} /> 19 </ScrollableContainer> 20 ); 21}
If you just want to track visibility, you can also use useTrackVisibility
hook.
It has the same API as useIntersectionObserver
hook. It just returns additional fields as its second tuple item.
1import React, { useEffect } from 'react'; 2import { useTrackVisibility } from 'react-intersection-observer-hook'; 3// ... 4 5function Example() { 6 // `useTrackVisibility` also returns a tuple like `useIntersectionObserver`. 7 // First item is the same `ref` callback to set the node to observe. 8 // Second item is an object that we can use to decide if a node is visible. 9 // `entry`: Same object which is returned by `useIntersectionObserver`. 10 // `rootRef`: Same ref callback which is returned by `useIntersectionObserver`. 11 // `isVisible`: Becomes `true`/`false` based on the response of `IntersectionObserver`. 12 // `wasEverVisible`: When the observed node becomes visible once, this flag becomes `true` and stays like that. 13 const [ref, { entry, rootRef, isVisible, wasEverVisible }] = 14 useTrackVisibility(); 15 16 useEffect(() => { 17 console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`); 18 }, [isVisible]); 19 20 return <SomeComponentToTrack ref={ref} />; 21}
You can find more usage examples in the demo
app in this repository.
Both useIntersectionObserver
and useTrackVisibility
gets the same arguments. And those are;
For more info, you can check here and here.
Thanks goes to these wonderful people (emoji key):
KimSeonghyeon 💻 | ||||||
Add your contributions |
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
5 existing vulnerabilities detected
Details
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More