Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/closeio/use-infinite-scroll

Super simple React hook for creating an infinite scroll experience based on the IntersectionObserver API
https://github.com/closeio/use-infinite-scroll

infinite-scroll react react-hooks react-infinite-scroll reactjs use-infinite-scroll

Last synced: about 1 month ago
JSON representation

Super simple React hook for creating an infinite scroll experience based on the IntersectionObserver API

Awesome Lists containing this project

README

        

# use-infinite-scroll

[![NPM](https://img.shields.io/npm/v/@closeio/use-infinite-scroll.svg)](https://www.npmjs.com/package/@closeio/use-infinite-scroll) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-success)](https://prettier.io)

Super simple React hook for creating an infinite scroll experience based on the `IntersectionObserver` API.

[Check the live DEMO](https://closeio.github.io/use-infinite-scroll/).

###

Interested in working on projects like this? [Close](https://close.com) is looking for [great engineers](https://jobs.close.com) to join our team!

## Install

```bash
yarn add @closeio/use-infinite-scroll
```

## Benefits

- Extremely lightweight (less than 1KB minzipped).
- It uses the `IntersectionObserver` API, so it doesn't need to listen to `scroll` events – which are known to cause performance issues.
- No other 3rd-party dependencies.

## Usage

```jsx
import React from 'react';
import useInfiniteScroll from '@closeio/use-infinite-scroll';

export default function MyComponent() {
const [items, setItems] = useState([]);
const [hasMore, setHasMore] = useState(false);
const [page, loaderRef, scrollerRef] = useInfiniteScroll({ hasMore });

useEffect(async () => {
const data = await myApiCall({ page });
setHasMore(data.hasMore);
setItems(prev => [...prev, data.items]);
}, [page]);

return (


{items.map(item => (
{item.name}

))}
{hasMore &&
Loading…
}

);
}
```

## License

MIT © [Close](https://github.com/closeio)