New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-easy-infinite-scroll-hook

Package Overview
Dependencies
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-easy-infinite-scroll-hook

A react hook for creating simple, fast and lightweight components with infinite scrolling in any direction, supporting both windowed and scrollable elements.

  • 1.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
decreased by-25.48%
Maintainers
1
Weekly downloads
 
Created
Source

react-easy-infinite-scroll-hook

Version Badge NPM license Test GZipped size PRs Welcome NPM total downloads Downloads

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.

Features

  • Universal - Ability to use all types of scrollable elements or any react-virtualized components
  • 📦 Support for all loading directions - You can infinitely scroll a component in any direction or in all directions at once (up, down, left, right)
  • 📏 No need to specify heights - No need to pass the dimensions of the component, scrollbar or element
  • 💬 Support for "chat history" - Reverse mode includes
  • 👫 Cross-browser - Works out-of-the-box for most browsers, regardless of version.
  • ⚙️ Matches native API - Intuitive to use
  • 🛠 Written in TypeScript - It'll fit right into your existing TypeScript project
  • 📲 Mobile-friendly - Supports mobile devices and touch screens.
  • Fully unit tested - 100% test coverage
  • 🌳 Tree-shakeable - Only include the parts you use
  • 💥 Lightweight - Around ~2kB
  • 💨 No dependencies

Install

  # with npm
  npm install --save react-easy-infinite-scroll-hook
  # with yarn
  yarn add react-easy-infinite-scroll-hook

Usage

You can create infinite scrolling in any direction and in any pair, for example: up-down, down-right, etc. and even all at once.

Simple Example

Edit useInfiniteScroll

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>
  );
};

Virtualized Example (react-virtualized)

This hook supports all react-virtualized components (Collection, Grid, MultiGrid, List, Masonry, Table).

Try it live:

ComponentDescriptionLink
ListVirtualized List component with infinite scrollEdit useInfiniteScroll
GridVirtualized Grid component with infinite scroll down and rightEdit useInfiniteScroll

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>
  );
};

API

After initialization, this hook returns a setRef function, which you must pass to your element ref.

Props

NameRequiredDescriptionTypeDefault Value
nextYesA callback when more items are requested by the user. Receives a single parameter specifying the direction to load e.g. (direction) => Promise<void>Function
hasMoreYesWhether 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
rowCountConditionNumber of items in a vertical list (scroll axis Y). Required if you are using vertical scroll.number
columnCountConditionNumber of items in a horizontal list (scroll axis X). Required if you are using horizontal scroll.number
onScrollThe callback is called when the container is scrolled: ({ clientHeight: number, scrollHeight: number, scrollTop: number, clientWidth: number, scrollWidth: number, scrollLeft: number }) => voidFunction
initialScrollThe initial scroll position of the element, which is applied after the ref has been initializedobject
reverseThe 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
scrollThresholdThe 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 string1

Friends

FAQ

Can I use it with flex-direction: 'column-reverse'?

Yes, just pass reverse: { column: true } to props for flex-direction: 'column-reverse' or reverse: { row: true } for flex-direction: 'row-reverse'.

How to use it with 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 the setRef 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>
  );
};

Contributions

Learn how to contribute

License

MIT © vdmrgv

Keywords

FAQs

Package last updated on 14 Jul 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc