Giter Site home page Giter Site logo

mintuz / react-intersect Goto Github PK

View Code? Open in Web Editor NEW
5.0 0.0 0.0 397 KB

A react component which observes when an element becomes in or out of the viewport you provided. Useful for lazy loading and sending tracking events to analytics on scroll.

JavaScript 100.00%

react-intersect's Introduction

React Intersect

A react component which observes when an element becomes in or out of the viewport you provided. Useful for lazy loading and sending tracking events to analytics on scroll.

Internally this makes use of another library I created called Horizon and tracking of elements is done via the Intersection Observer API allowing for performant tracking on the web as it's done off the main thread reducing jank whilst scrolling.

This component makes use of the render props and state reducer patterns

Install

yarn add @mintuz/react-intersect --save

Usage

Config

Key Value Description Default
onEntry (entry) => {} Callback which is called when the element is in view N/A
onExit (entry) => {} Callback which is called when the element is out of view N/A
triggerOnce true Will trigger onEntry callback once, useful for lazyLoading false
render (inView) => {return <div></div>} Render prop that returns a single boolean of if in view or not, use to conditionally load an element when in view N/A
intersectionObserverConfig { root: null, rootMargin: '35%', threshold: 0 } Options passed to IntersectionObserver API { root: null rootMargin: '35%', threshold: 0 }
stateReducer (prevState, changes) => {return {inView:false}} Allow for external manipulation of the internal state of react-intersect N/A

Hook API

const ReactIntersectHook = () => {
  const [inView, ref] = useIntersect({
    triggerOnce: false
  });
  return (
    <div ref={ref}>
      {inView ? "I am in view " : "I am not view"}
    </div>
  );
};

Demo

https://codesandbox.io/s/vvm485k697

react-intersect's People

Contributors

mintuz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

react-intersect's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.