![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
react-easy-infinite-scroll-hook
Advanced tools
A react hook for creating simple, fast and lightweight components with infinite scrolling in any direction, supporting both windowed and scrollable elements.
A hook that will save you from endless scrolling problems! Infinite scrolling that really works and is very easy to integrate! This hook allows you to create simple, lightweight components with infinite scrolling in all directions, supporting both windowed and scrollable elements.
up
, down
, left
, right
)100%
test coverage~2kB
# with npm
npm install --save react-easy-infinite-scroll-hook
# with yarn
yarn add react-easy-infinite-scroll-hook
You can create infinite scrolling in any direction
and in any pair
, for example: up-down
, down-right
, etc.
and even all at once
.
Try it live:
Name | Description | Link |
---|---|---|
Simple List | Simple list component with infinite scroll down | |
Window List | Infinite scroll list that uses the window's scroll as its container |
import useInfiniteScroll from 'react-easy-infinite-scroll-hook';
const InfiniteListComponent = ({ isLoading, items, canLoadMore, next }) => {
const { setRef } = useInfiniteScroll({
// Function to fetch more items
next,
// The number of items loaded if you use the "Y-scroll" axis ("up" and "down")
// if you are using the "X-scroll" axis ("left" and "right") use "columnCount" instead
// you can also use "rowCount" and "columnCount" if you use "Y-scroll" and "X-scroll" at the same time
rowCount: items.length,
// Whether there are more items to load
// if marked "true" in the specified direction, it will try to load more items if the threshold is reached
// support for all directions "up", "down", "left", "right", both individually and in all directions at the same time
hasMore: { down: canLoadMore },
});
return (
<div
ref={setRef}
style={{
height: 500,
overflowY: 'auto',
}}
>
{items.map((item) => (
<div key={item.key}>{item.value}</div>
))}
{isLoading && <div>Loading...</div>}
</div>
);
};
This hook supports all react-virtualized components (Collection
, Grid
, MultiGrid
, List
, Masonry
, Table
).
Try it live:
Component | Description | Link |
---|---|---|
List | Virtualized List component with infinite scroll | |
Grid | Virtualized Grid component with infinite scroll down and right |
import useInfiniteScroll from 'react-easy-infinite-scroll-hook';
import { List } from 'react-virtualized';
const VirtualizedInfiniteListComponent = ({ isLoading, items, canLoadMore, next }) => {
const { setRef } = useInfiniteScroll({
next,
rowCount: items.length,
hasMore: { down: canLoadMore },
});
return (
<div>
<List
ref={setRef}
width={500}
height={500}
rowHeight={60}
rowCount={items.length}
rowRenderer={({ key, index, style }) => {
const item = data[index];
return (
<div key={key} style={style}>
{item}
</div>
);
}}
/>
{isLoading && <div>Loading...</div>}
</div>
);
};
After initialization, this hook returns a setRef
function, which you must pass to your element ref
.
Name | Required | Description | Type | Default Value |
---|---|---|---|---|
next | Yes | A callback when more items are requested by the user. Receives a single parameter specifying the direction to load e.g. (direction) => Promise<void> | Function | |
hasMore | Yes | Whether there are more items to load. If marked true in the specified direction, it will try to load more items if the threshold is reached. Expect object with directions to load { up: false, down: false, left: false, right: false } | object | |
rowCount | Condition | Number of items in a vertical list (scroll axis Y ). Required if you are using vertical scroll. | number | |
columnCount | Condition | Number of items in a horizontal list (scroll axis X ). Required if you are using horizontal scroll. | number | |
onScroll | The callback is called when the container is scrolled: ({ clientHeight: number, scrollHeight: number, scrollTop: number, clientWidth: number, scrollWidth: number, scrollLeft: number }) => void | Function | ||
initialScroll | The initial scroll position of the element, which is applied after the ref has been initialized | object | ||
reverse | The direction of the scroll axis is used to create scrolling in the opposite direction, for example when using the CSS style flex-direction: 'row-reverse' | object | ||
scrollThreshold | The threshold at which the next function is called. It can be specified in pixels from the scrollbar value, for example '200px' and as a percentage of the container size from 0.1 to 1 (1 is 100% ) | number or string | 1 | |
windowScroll | When set to true , uses a window as the scroll element. If you are using a scroll window, then anything you pass to the setRef function will be ignored | boolean | false |
Yes you can! To use it with other libraries you must specify the correct DOM element for the
setRef
function.
flex-direction: 'column-reverse'
?Yes, just pass
reverse: { column: true }
to props forflex-direction: 'column-reverse'
orreverse: { row: true }
forflex-direction: 'row-reverse'
.
react-virtualized
MultiGrid
component?
MultiGrid
is a complex component with a lot of scrollable containers, and to use it you must specify the correct container for thesetRef
function:
import React, { useCallback } from 'react';
import useInfiniteScroll from 'react-easy-infinite-scroll-hook';
import { MultiGrid } from 'react-virtualized';
const VirtualizedInfiniteMultiGridComponent = ({ isLoading, items, canLoadMore, next }) => {
const { setRef } = useInfiniteScroll({
next,
columnCount: items.length,
hasMore: { right: canLoadMore },
});
// Use `useCallback` so we don't recreate the function on each render - Could result in infinite loop
const selectRef = useCallback(
(node) => {
setRef(node._bottomRightGrid);
},
[setRef]
);
return (
<div>
<MultiGrid ref={selectRef} {...props} />
{isLoading && <div>Loading...</div>}
</div>
);
};
Please read through our contributing guidelines. Included are directions for opening issues, coding standards, and notes on development.
Code released under the MIT License © Vadim Rogov.
FAQs
A react hook for creating simple, fast and lightweight components with infinite scrolling in any direction, supporting both windowed and scrollable elements.
The npm package react-easy-infinite-scroll-hook receives a total of 1,684 weekly downloads. As such, react-easy-infinite-scroll-hook popularity was classified as popular.
We found that react-easy-infinite-scroll-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.