All Projects â†’ onderonur â†’ react-intersection-observer-hook

onderonur / react-intersection-observer-hook

Licence: MIT license
React hook to use IntersectionObserver declaratively

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to react-intersection-observer-hook

React Intersection Observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.
Stars: ✭ 2,689 (+4536.21%)
Mutual labels:  visibility, intersection-observer, intersectionobserver
svelte-inview
A Svelte action that monitors an element enters or leaves the viewport.🔥
Stars: ✭ 358 (+517.24%)
Mutual labels:  intersection-observer, intersectionobserver
react-intersection-observer-api-example
Showcasing of the Intersection Observer API in React with createRef()
Stars: ✭ 13 (-77.59%)
Mutual labels:  intersection-observer, intersectionobserver
bs-react-is-visible
A small library that lets you know whether a component is visible on screen or not.
Stars: ✭ 15 (-74.14%)
Mutual labels:  visibility, intersection-observer
react-class-hooks
React Hooks implementation for Class Components. Support React >= 15.3.2
Stars: ✭ 63 (+8.62%)
Mutual labels:  react-hooks
react-hook-videojs
Easy React integration of Video.js using hooks.
Stars: ✭ 37 (-36.21%)
Mutual labels:  react-hooks
shaka-player-react
A simple React component wrapper for shaka-player
Stars: ✭ 79 (+36.21%)
Mutual labels:  react-hooks
veact
Mutable state enhancer library for React based on @vuejs
Stars: ✭ 62 (+6.9%)
Mutual labels:  react-hooks
vue-observable
IntersectionObserver, MutationObserver and PerformanceObserver in Vue.js
Stars: ✭ 24 (-58.62%)
Mutual labels:  intersection-observer
mint-ui
Design System | React UI components for web
Stars: ✭ 17 (-70.69%)
Mutual labels:  react-hooks
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-62.07%)
Mutual labels:  react-hooks
use-key-hook
React hook to handle all the key press.
Stars: ✭ 27 (-53.45%)
Mutual labels:  react-hooks
monolazy
Extended nanocomponent providing onEnter callback
Stars: ✭ 21 (-63.79%)
Mutual labels:  intersection-observer
react-headless-tabs
Headless and highly flexible tab-like primitives built with react hooks
Stars: ✭ 107 (+84.48%)
Mutual labels:  react-hooks
use-algolia
Dead-simple React hook to make Algolia search queries. Supports pagination out of the box.
Stars: ✭ 29 (-50%)
Mutual labels:  react-hooks
add-my-name
No more WhatsApp spams 🎉
Stars: ✭ 16 (-72.41%)
Mutual labels:  react-hooks
Websocket-React-Chatroom
A chatroom powered by React-Hooks and Websocket🚀
Stars: ✭ 76 (+31.03%)
Mutual labels:  react-hooks
react-app-simple-chat-app
A Simple Chat Application using MERN stack (MongoDB, Express JS, React JS, Node JS) and Socket.io for real time chatting
Stars: ✭ 41 (-29.31%)
Mutual labels:  react-hooks
react-register-nodes
Register DOM Nodes in React
Stars: ✭ 32 (-44.83%)
Mutual labels:  react-hooks
realar
5 kB Advanced state manager for React
Stars: ✭ 41 (-29.31%)
Mutual labels:  react-hooks

react-intersection-observer-hook

All Contributors

Build status License Version

This is a small React hook package to use 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 screen etc.

This package is written in TypeScript (all thanks to TSDX). So, you can use it in your TypeScript projects too.

Live demo is here.

You can check the browser compatibility from here.

If you want to support the browsers those are not supporting it natively, you can use this polyfill.

Installation

npm install react-intersection-observer-hook

Usage

Here is a simple code to use the hook. Just pass the ref callback to the component that you want to track its visibility. You can find a more complete code in the example folder.

import React, { useEffect } from 'react';
import { useIntersectionObserver } from 'react-intersection-observer-hook';
// ...

function Example() {
  // `useIntersectionObserver` returns a tuple.
  // We need to give this `ref` callback to the node we want to observe.
  // The second item, `entry` is the response of the initially created `IntersectionObserver` instance.
  const [ref, { entry }] = useIntersectionObserver();
  const isVisible = entry && entry.isIntersecting;

  useEffect(() => {
    console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
  }, [isVisible]);

  return <SomeComponentToTrack ref={ref} />;
}

if you have a scrollable container, you can set a root like this:

import React, { useEffect } from 'react';
import { useIntersectionObserver } from 'react-intersection-observer-hook';
// ...

function Example() {
  const [ref, { entry, rootRef }] = useIntersectionObserver();
  const isVisible = entry && entry.isIntersecting;

  useEffect(() => {
    console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
  }, [isVisible]);

  return (
    <ScrollableContainer
      // We use `rootRef` callback to set our root node.
      ref={rootRef}
    >
      <SomeComponentToTrack ref={ref} />
    </ScrollableContainer>
  );
}

If you just want to track visibility, you can use useTrackVisibility hook. It has the same API as useIntersectionObserver hook. It just returns additional fields in its second tuple item.

import React, { useEffect } from 'react';
import { useTrackVisibility } from 'react-intersection-observer-hook';
// ...

function Example() {
  // `useTrackVisibility` also returns a tuple like `useIntersectionObserver`.
  // First item is the same `ref` callback to set the node to observe.
  // Second item is an object that we can use to decide if a node is visible.
  // `entry`: Same object which is returned by `useIntersectionObserver`.
  // `rootRef`: Same ref callback which is returned by `useIntersectionObserver`.
  // `isVisible`: Becomes true/false based on the response of `IntersectionObserver`.
  // `wasEverVisible`: When our observed node becomes visible once, this flag becomes `true` and stays like that.
  const [
    ref,
    { entry, rootRef, isVisible, wasEverVisible },
  ] = useTrackVisibility();

  useEffect(() => {
    console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
  }, [isVisible]);

  return <SomeComponentToTrack ref={ref} />;
}

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

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].