1.2.2 • Published 3 years ago

react-useonview v1.2.2

Weekly downloads
7
License
MIT
Repository
github
Last release
3 years ago

react-useonview

A react hook that returns a ref and does sth. when the element attached to that ref is scrolled into view.

NPM JavaScript Style Guide

A react hook to trigger afunction when an component is scrolled into view. The given function will also be triggered on every subsequent scroll. So maybe add a closure to prevent this.

Install

npm install --save react-useonview

or

yarn add react-useonview

Usage

import React, { Component } from 'react';

import useOnView from 'react-useonview';

const App = () => {
  const [visible, setVisible] = useState(false);

  const trigger = useOnView(() => setVisible(true));

  return (
    <div>
      <div style={{ height: '100vh' }}></div>
      <div
        ref={trigger}
        style={{ opacity: visible ? '1' : '0', transition: '1s' }}
      >
        I appear when scrolled into view
      </div>
    </div>
  );
};

notes

  • the function will probably execute several times

  • does not care whether the element which has the ref attached is actually visible

  • also executes when scrolled past element

Api

Parameter in orderDescription
doSthfunction that is executed when ref-element scrolled in or past view
fullViewshould the object be in fullview to trigger doSth

options

namevaluesdefaultdescription
fullViewbooleanfalseshould doSth be triggered at first contact or when the element is in full view?

License

MIT © JustinHorn

1.2.0

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago