# react-scroll-viewport

> Only renders items visible in the current viewport.

Latest version **0.3.0** (published 2017-11-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-scroll-viewport
pnpm add react-scroll-viewport
yarn add react-scroll-viewport
bun add react-scroll-viewport
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2017-11-12 |
| First published | 2017-11-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | donniewest |
| Keywords | react, virtual, viewport, virtualized, scroll |

## Links

- npm: https://www.npmjs.com/package/react-scroll-viewport
- Repository: https://github.com/DonnieWest/react-scroll-viewport
- Issues: https://github.com/DonnieWest/react-scroll-viewport/issues
- npm.io page: https://npm.io/package/react-scroll-viewport

## 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

- 0.3.0 (latest) — 2017-11-12
- 0.2.2 — 2017-11-12
- 0.2.1 — 2017-11-12
- 0.2.0 — 2017-11-12

## README

# `<ScrollViewport />` <sub>_for [Preact]_</sub>

[![NPM](https://img.shields.io/npm/v/preact-scroll-viewport.svg)](https://www.npmjs.com/package/preact-scroll-viewport)
[![Travis](https://travis-ci.org/developit/preact-scroll-viewport.svg?branch=master)](https://travis-ci.org/developit/preact-scroll-viewport)

A compositional component that renders its children based on the current viewport.

Useful for those super important business applications where one must show _all_ million rows.

#### [CodeSandbox Demo](https://codesandbox.io/s/j70zm85py9)

<a href="https://codesandbox.io/s/j70zm85py9">
	<img alt="preview" src="https://i.gyazo.com/38f75b5db9615b0a08304d6cca209e47.gif" width="420">
</a>

---


## Usage Example

Simply wrap a large collection of children in this component, and they will be rendered based on the viewport.
You can define a default row height (`defaultRowHeight`) to use prior to dimensions being available, or a static row height (`rowHeight`) to avoid style recalculation entirely. If `rowHeight` is not provided, the height of the first row will be calculated and extrapolated.

```js
// create 100,000 children:
let children = [];
for (let i=1; i<100000; i++) {
	children.push(<div class="row">{i}</div>);
}

// ...but only render what is in-viewport:
render(
	<ScrollViewport rowHeight={22}>
		{children}
	</ScrollViewport>
);
```


---


## Props

| Prop                  | Type       | Description         |
|-----------------------|------------|---------------------|
| **`rowHeight`**        | _Number_   | Static height of a row (prevents style recalc)
| **`defaultRowHeight`** | _Number_   | Initial height of a row prior to dimensions being available
| **`overscan`**         | _Number_   | Number of extra rows to render above and below visible list. Defaults to 10. \*
| **`sync`**             | _Boolean_  | If `true`, forces synchronous rendering \*\*

_**\* Why overscan?** Rendering normalized blocks of rows reduces the number of DOM interactions by grouping all row renders into a single atomic update._

_**\*\* About synchronous rendering:** It's best to try without `sync` enabled first. If the normal async rendering behavior is fine, leave sync turned off. If you see flickering, enabling sync will ensure every update gets out to the screen without dropping renders, but does so at the expense of actual framerate._


| _Without_ Overscan | _With_ Overscan |
|--------------------|-----------------|
| <img src="https://i.gyazo.com/e192bf1ca835fbe6ad803f7b6270e424.gif" height="150"> | <img src="https://i.gyazo.com/478440d1f06fe543e69fff8b88ce7963.gif" height="150"> |


---

## Simple Example

[**View this example on CodeeSandbox**](https://codesandbox.io/s/j70zm85py9)
```js
import ScrollViewport from 'react-scroll-viewport';

class Demo extends Component {
    // 30px tall rows
    rowHeight = 30;

    render() {
		// Generate 100,000 rows of data
		let rows = [];
		for (let x=1e5; x--; ) rows[x] = `Item #${x+1}`;

        return (
            <ScrollViewport class="list" rowHeight={this.rowHeight}>
				{ rows.map( row => (
					<div class="row">{row}</div>
				)) }
			</ScrollViewport>
        );
    }
}

render(Demo, document.body);
```


---


### License

[MIT]

[Original]: https://github.com/developit/preact-scroll-viewport
[MIT]: http://choosealicense.com/licenses/mit/

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