npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

reactjs-scroll-pagination

v6.0.2

Published

A React component for infinite scroll pagination that works seamlessly with React and Next.js applications

Downloads

37

Readme

A powerful, lightweight React component for infinite scroll pagination that works seamlessly with React and Next.js applications. Features include error handling, retry mechanism, custom loaders, scroll direction detection, and much more!

✨ Features

  • 🚀 Next.js SSR Compatible - Works perfectly with server-side rendering
  • 🎯 TypeScript Support - Fully typed for better developer experience
  • Performance Optimized - Uses IntersectionObserver API for efficient scroll detection
  • 🔄 Error Handling - Built-in error handling with retry functionality
  • 🎨 Customizable - Highly customizable loaders, messages, and styles
  • 📱 Mobile Friendly - Works great on all devices
  • 🔍 Scroll Direction Detection - Load more based on scroll direction
  • ⏱️ Debouncing - Prevent multiple rapid API calls
  • 🔁 Reverse Mode - Load content at the top (useful for chat apps)
  • 🎭 Custom Loaders - Use your own loading components
  • 💪 Lightweight - No heavy dependencies

Install

npm install reactjs-scroll-pagination

or

yarn add reactjs-scroll-pagination

Basic Usage

import React, { useState } from 'react';
import ScrollPagination from 'reactjs-scroll-pagination';

const MyComponent = () => {
  const [data, setData] = useState([1, 2, 3, 4, 5]);
  const [hasMore, setHasMore] = useState(true);

  const loadMore = () => {
    // Simulate API call
    setTimeout(() => {
      const newData = [...data, ...Array(5).fill(0).map((_, i) => data.length + i + 1)];
      setData(newData);
      if (newData.length >= 50) setHasMore(false);
    }, 1000);
  };

  return (
    <div>
      <ScrollPagination 
        loading={<div>Loading more...</div>} 
        loadMore={loadMore} 
        hasMore={hasMore}
      >
        {data.map((item, index) => (
          <div key={index}>Item {item}</div>
        ))}
      </ScrollPagination>
    </div>
  );
};

export default MyComponent;

Advanced Usage with Next.js

'use client'; // For Next.js 13+ App Router

import React, { useState } from 'react';
import ScrollPagination from 'reactjs-scroll-pagination';

const ProductList = () => {
  const [products, setProducts] = useState([]);
  const [page, setPage] = useState(1);
  const [hasMore, setHasMore] = useState(true);

  const loadMore = async () => {
    try {
      const response = await fetch(`/api/products?page=${page}`);
      const newProducts = await response.json();
      
      setProducts([...products, ...newProducts]);
      setPage(page + 1);
      
      if (newProducts.length < 10) {
        setHasMore(false);
      }
    } catch (error) {
      console.error('Failed to load products:', error);
      throw error; // Will trigger error handling
    }
  };

  return (
    <ScrollPagination 
      loadMore={loadMore}
      hasMore={hasMore}
      loader={<div className="spinner">Loading...</div>}
      endMessage={<p>No more products to load!</p>}
      threshold={0.5}
      rootMargin="200px"
      debounceMs={300}
      onError={(error) => console.error('Error loading data:', error)}
      retryOnError={true}
    >
      <div className="grid grid-cols-3 gap-4">
        {products.map((product) => (
          <ProductCard key={product.id} product={product} />
        ))}
      </div>
    </ScrollPagination>
  );
};

API Reference

Props

| Prop | Type | Required | Default | Description | |------|------|----------|---------|-------------| | children | ReactNode | Yes | - | The content to be rendered | | loadMore | () => void | Promise | Yes | - | Function to load more data | | hasMore | boolean | Yes | - | Whether there is more data to load | | loading | ReactNode | No | "Loading more..." | Loading message/component | | loader | ReactNode | (() => ReactNode) | No | null | Custom loader component | | endMessage | ReactNode | (() => ReactNode) | No | null | Message shown when no more data | | threshold | number | No | 0.1 | Intersection observer threshold (0-1) | | rootMargin | string | No | "10px" | Margin around the root element | | reverse | boolean | No | false | Load content at the top instead of bottom | | scrollDirection | 'up' | 'down' | 'both' | No | 'down' | Trigger loading based on scroll direction | | onError | (error: Error) => void | No | null | Error callback function | | retryOnError | boolean | No | false | Show retry button on error | | className | string | No | "" | CSS class for the wrapper div | | loaderClassName | string | No | "" | CSS class for the loader div | | initialLoad | boolean | No | false | Trigger loadMore on component mount | | debounceMs | number | No | 0 | Debounce time in milliseconds |

Examples

With Custom Loader

<ScrollPagination 
  loadMore={loadMore}
  hasMore={hasMore}
  loader={
    <div className="flex justify-center">
      <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
    </div>
  }
>
  {children}
</ScrollPagination>

With End Message

<ScrollPagination 
  loadMore={loadMore}
  hasMore={hasMore}
  endMessage={
    <div className="text-center py-4">
      <p>🎉 You've reached the end!</p>
    </div>
  }
>
  {children}
</ScrollPagination>

With Error Handling and Retry

<ScrollPagination 
  loadMore={loadMore}
  hasMore={hasMore}
  onError={(error) => {
    console.error('Load failed:', error);
    // Send to error tracking service
  }}
  retryOnError={true}
>
  {children}
</ScrollPagination>

Reverse Mode (Chat Application)

<ScrollPagination 
  loadMore={loadMoreMessages}
  hasMore={hasMore}
  reverse={true}
  scrollDirection="up"
>
  {messages.map(msg => <Message key={msg.id} {...msg} />)}
</ScrollPagination>

With Debouncing

<ScrollPagination 
  loadMore={loadMore}
  hasMore={hasMore}
  debounceMs={500}
  threshold={0.5}
  rootMargin="100px"
>
  {children}
</ScrollPagination>

With Initial Load

<ScrollPagination 
  loadMore={loadMore}
  hasMore={hasMore}
  initialLoad={true}
>
  {children}
</ScrollPagination>

TypeScript Support

This package is written in TypeScript and provides full type definitions:

import ScrollPagination, { ScrollPaginationProps } from 'reactjs-scroll-pagination';

const MyComponent: React.FC = () => {
  const props: ScrollPaginationProps = {
    loadMore: async () => { /* ... */ },
    hasMore: true,
    children: <div>Content</div>
  };

  return <ScrollPagination {...props} />;
};

Browser Support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Edge (latest)
  • Mobile browsers

Requires support for IntersectionObserver API (available in all modern browsers).

Contributing

Contributions, issues and feature requests are welcome!

Author

Show your support

Give a ⭐️ if this project helped you!

📝 License

This project is ISC licensed.


This README was generated with ❤️ by readme-md-generator