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

This hook allows you to create simple, lightweight components with infinite scrolling in all directions, supporting both windowed and scrollable elements.

  • 1.0.3
  • 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

Test GZipped size Version Badge 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 react-virtualized components
  • 📦 Support for all loading directions - You can scroll the component indefinitely in the direction you want or all 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
  • ⚙️ Matches native API - Intuitive to use
  • 🛠 Written in TypeScript - It'll fit right into your existing TypeScript project
  • Fully unit tested - 100% test coverage
  • 🌳 Tree-shakeable - Only include the parts you use
  • 💥 Lightweight - Around ~1.9kB

Install

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

Usage

Simple Example

Edit useInfiniteScroll

import { useInfiniteScroll } from 'react-easy-infinite-scroll-hook';

const InfiniteListComponent = ({ isLoading, items, canLoadMore, next }) => {
  const { setRef } = useInfiniteScroll({
    next,
    rowLength: items.length,
    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)

Edit useInfiniteScroll

import { useInfiniteScroll } from 'react-easy-infinite-scroll-hook';
import { List } from 'react-virtualized';

const VirtualizedInfiniteListComponent = ({ isLoading, items, canLoadMore, next }) => {
  const { setRef } = useInfiniteScroll({
    next,
    rowLength: items.length,
    hasMore: { down: canLoadMore },
  });

  return (
    <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>
        );
      }}
    />
  );
};

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 be loaded. Expect object with directions to load { up: false, down: false, left: false, right: false }object
rowLengthConditionNumber of items in a vertical list (scroll axis Y). Required if you are using vertical scroll.number
columnLengthConditionNumber of items in a horizontal list (scroll axis X). Required if you are using horizontal. scrollnumber
onScrollThe callback is called when the container is scrolled: ({ clientHeight: number, scrollHeight: number, scrollTop: 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 element value 0.6 = 60%number or string1

Friends

FAQ

  1. Can I use it with flex-direction: 'row-reverse'?
  • Yes, just pass reverse: { vertical: true } to the props.

Troubleshooting

  1. What should I do if I have an endless call next function?
  • Try checking your element and make sure it has a fixed size (the size does not increase after receiving new data, only the size of the scrollbar increases) and the overflow can be scrolled in the right direction.

Keywords

FAQs

Package last updated on 19 Jun 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