React hook to use IntersectionObserver declaratively.
Installations
npm install react-intersection-observer-hook
Developer
Developer Guide
Module System
CommonJS, ESM
Min. Node Version
Typescript Support
Yes
Node Version
20.16.0
NPM Version
10.8.2
Statistics
73 Stars
58 Commits
7 Forks
1 Watching
1 Branches
1 Contributors
Updated on 20 Nov 2024
Bundle Size
1.53 kB
Minified
691.00 B
Minified + Gzipped
Languages
TypeScript (71.19%)
JavaScript (28.46%)
CSS (0.35%)
Total Downloads
Cumulative downloads
Total Downloads
10,517,486
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
react-intersection-observer-hook
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.
Installation
1npm install react-intersection-observer-hook
Usage
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.
Arguments
Both useIntersectionObserver
and useTrackVisibility
gets the same arguments. And those are;
- rootMargin: Indicates the margin value around the root element. Default value is zero for all directions (top, right, bottom and left).
- threshold: Threshold value (or values) to trigger the observer.
For more info, you can check here and here.
Contributors ✨
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
5 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-g77x-44xx-532m
- Warn: Project is vulnerable to: GHSA-gp8f-8m3g-qvj9
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
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
- Info: topLevel 'contents' permission set to 'read': .github/workflows/nextjs.yml:20
- Warn: no topLevel permission defined: .github/workflows/quality.yml:1
- Info: no jobLevel write permissions found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nextjs.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/react-intersection-observer-hook/nextjs.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nextjs.yml:43: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/react-intersection-observer-hook/nextjs.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nextjs.yml:49: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/react-intersection-observer-hook/nextjs.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nextjs.yml:63: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/react-intersection-observer-hook/nextjs.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:55: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/react-intersection-observer-hook/quality.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/react-intersection-observer-hook/quality.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/react-intersection-observer-hook/quality.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/quality.yml:44: update your workflow using https://app.stepsecurity.io/secureworkflow/onderonur/react-intersection-observer-hook/quality.yml/main?enable=pin
- Info: 0 out of 8 GitHub-owned GitHubAction dependencies pinned
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Score
3.2
/10
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 MoreOther packages similar to 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