# react-useonview

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

Latest version **1.2.2** (published 2021-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-useonview
pnpm add react-useonview
yarn add react-useonview
bun add react-useonview
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2021-05-05 |
| First published | 2020-11-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Justin Horn |
| Maintainers | murtag |
| Keywords | react, onView, hooks |

## Links

- npm: https://www.npmjs.com/package/react-useonview
- Repository: https://github.com/JustinHorn/react-useonview
- Homepage: https://github.com/JustinHorn/react-useonview#readme
- Issues: https://github.com/JustinHorn/react-useonview/issues
- npm.io page: https://npm.io/package/react-useonview

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.2 (latest) — 2021-05-05
- 1.2.1 — 2021-05-05
- 1.2.0 — 2021-05-05
- 1.1.3 — 2020-11-05
- 1.1.2 — 2020-11-05
- 1.1.1 — 2020-11-05
- 1.0.1 — 2020-11-05
- 1.0.0 — 2020-11-05

## README

# react-useonview

> A react hook that returns a ref and does sth. when the element attached to that ref is scrolled into view.

[![NPM](https://img.shields.io/npm/v/react-useonview.svg)](https://www.npmjs.com/package/react-useonview) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

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

```bash
npm install --save react-useonview
```

or

```bash
yarn add react-useonview
```

## Usage

```jsx
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 order | Description                                                         |
| ------------------ | ------------------------------------------------------------------- |
| doSth              | function that is executed when ref-element scrolled in or past view |
| fullView           | should the object be in fullview to trigger doSth                   |

### options

| name     | values  | default | description                                                                     |
| -------- | ------- | ------- | ------------------------------------------------------------------------------- |
| fullView | boolean | false   | should doSth be triggered at first contact or when the element is in full view? |

## License

MIT © [JustinHorn](https://github.com/JustinHorn)

---
_Source: https://npm.io/package/react-useonview · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
