react-intersection-observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport. Contains both a Hooks, render props and plain children implementation.
Storybook Demo: https://react-intersection-observer.vercel.app
Features
-
🪝 Hooks or Component API - WithuseInView
it's easier than ever to monitor elements -
⚡️ Optimized performance - Reuses Intersection Observer instances where possible -
⚙️ Matches native API - Intuitive to use -
🛠 Written in TypeScript - It'll fit right into your existing TypeScript project -
🧪 Ready to test - Mocks the Intersection Observer for easy testing with Jest -
🌳 Tree-shakeable - Only include the parts you use -
💥 Tiny bundle - Around ~1.15kB foruseInView
and ~1.6kB for<InView>
Installation
Install using Yarn:
yarn add react-intersection-observer
or NPM:
npm install react-intersection-observer --save
Usage
useInView
hook
// Use object destructing, so you don't need to remember the exact order
const { ref, inView,
…