Gathering detailed insights and metrics for react-hook-inview
Gathering detailed insights and metrics for react-hook-inview
Gathering detailed insights and metrics for react-hook-inview
Gathering detailed insights and metrics for react-hook-inview
react-cool-inview
React hook to monitor an element enters or leaves the viewport (or another element).
react-inview-hook
react hooks & intersection-observer
@1stmmd/react-in-view-hook
just a hook who when used return a ref and inView variable
target-observer
With Target Observer, you can easily trigger animations, lazy load content, or handle visibility-based logic in your application.
npm install react-hook-inview
Typescript
Module System
Node Version
NPM Version
74.2
Supply Chain
94.1
Quality
77
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
2,354,555
Last Day
658
Last Week
7,287
Last Month
44,780
Last Year
475,164
86 Stars
104 Commits
5 Forks
2 Watching
2 Branches
3 Contributors
Latest Version
4.5.1
Package Id
react-hook-inview@4.5.1
Unpacked Size
16.83 kB
Size
5.17 kB
File Count
12
NPM Version
10.5.0
Node Version
20.12.2
Publised On
29 Apr 2024
Cumulative downloads
Total Downloads
Last day
-66.9%
658
Compared to previous day
Last week
-33.4%
7,287
Compared to previous week
Last month
22.5%
44,780
Compared to previous month
Last year
-27.3%
475,164
Compared to previous year
22
Detect if an element is in the viewport using a React Hook. Utilizes the Intersection Observer API, so check for compatibility.
npm install react-hook-inview
Optional: Install a polyfill for browsers that don't support
IntersectionObserver
yet (i.e. Safari 12).
useInView
The hook in its most basic form returns a ref and a boolean.
1const [ref, inView] = useInView()
That's all you need to get started, but it does a lot more.
In this example, the boolean is used to toggle some text on and off when the element is fully in the viewport.
1import React from 'react' 2import { useInView } from 'react-hook-inview' 3 4const Component = () => { 5 const [ref, isVisible] = useInView({ 6 threshold: 1, 7 }) 8 9 return <div ref={ref}>{isVisible ? 'Hello World!' : ''}</div> 10}
The hook returns a tuple with four items:
ref
callback, used to set observer on an element.boolean
when the element is in the viewport.IntersectionObserverEntry
IntersectionObserver
itself1const [ref, inView, entry, observer] = useInView(options, [...state])
These are the default options.
1{ 2 root?: RefObject<Element> | null, // Optional, must be a parent of your ref 3 rootMargin?: string, // '0px' or '0px 0px 0px 0px', also accepts '%' unit 4 threshold?: number | number[], // 0.5 or [0, 0.5, 1] 5 unobserveOnEnter?: boolean, // Set 'true' to run only once 6 onEnter?: (entry?, observer?) => void, // See below 7 onLeave?: (entry?, observer?) => void, // See below 8 target?: RefObject<Element> | null, // *DEPRECATED* Supply your own ref object 9 defaultInView?: boolean, // false 10}
NOTE If you're updating from < version 4.0.0.
, you might have noticed an
API changed. The target
option has been deprecated, but still works the same
way.
onEnter
& onLeave
callbacks:warning: These options are deprecated, and support may be removed in a future release. To access the intersection observer callback, use the useInViewEffect hook instead.
onEnter
and onLeave
recieve a callback function that returns an
IntersectionObserverEntry
and the IntersectionObserver
itself. The two
arguments are entirely optional.
1function onEnter(entry, observer) { 2 // entry.boundingClientRect 3 // entry.intersectionRatio 4 // entry.intersectionRect 5 // entry.isIntersecting 6 // entry.rootBounds 7 // entry.target 8 // entry.time 9}
NOTE: If you supply an array with multiple values to threshold
, onEnter
will be called each time the element intersects with the top and bottom of
the viewport. onLeave
will on trigger once the element has left the viewport
at the first threshold specified.
For performance reasons, the hook is only triggered once on mount. However,
this means you can't access updated state in the onEnter/onLeave
callbacks.
An optional second argument will retrigger the hook to mitigate this.
1// Some other state 2const [state, setState] = useState(false) 3 4const [ref, inView] = useInView( 5 { 6 onEnter: () => console.log(state), 7 }, 8 [state], // <- Will update callback 9)
This will remount the intersection observer, and may have unintended side effects. Use this feature with caution.
useInViewEffect
An alternate hook that allows you to just supply the intersection observer callback. This approach is gives you a little more flexibilty than using the callbacks in the original hook as it doesn't obfuscate the Intersection Observer API as much.
1const ref = useInViewEffect(callback, options, [...state])
1import React, { useState } from 'react' 2import { useInViewEffect } from 'react-hook-inview' 3 4const Component = () => { 5 const [isVisible, setIsVisible] = useState(false) 6 7 const ref = useInViewEffect( 8 ([entry], observer) => { 9 if (entry.isIntersecting) { 10 observer.unobserve(entry.target) 11 } 12 setIsVisible(entry.isIntersecting) 13 }, 14 { threshold: 0.5 }, 15 ) 16 17 return <div ref={ref}>{isVisible ? 'Hello World!' : ''}</div> 18}
Keep in mind that the first argument will return an array.
The useInViewEffect
hook has more limited options that mirror the default
API.
1{ 2 root?: RefObject<Element> | null, // Optional, must be a parent of your ref 3 rootMargin?: string, // '0px' or '0px 0px 0px 0px', also accepts '%' unit 4 threshold?: number | number[], // 0.5 or [0, 0.5, 1] 5}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/23 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
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-12-23
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