react-use-viewability
React hook for tracking component viewability, based on IntersectionObserver.
Installation
$ npm install react-use-viewability --save-dev
Usage
The hook accepts the standard IntersectionObserver options and returns an array containing an inView
value that always reflects the component's viewability, and an inViewRef
ref that must be passed to the element you want to track.
Example (try the live version):
;; const Box = { // Consider the element viewable when at least half of it is in the viewport const inView inViewRef = ; return <div // Pass the received ref to the element ref=inViewRef // Change background color when entering or leaving the viewport style= backgroundColor: inView ? 'green' : 'red' /> ;}; ;
License
MIT License