0.0.3 • Published 8 months ago

react-scroll-pagify v0.0.3

Weekly downloads
-
License
-
Repository
github
Last release
8 months ago

A lightweight React component that provides infinite scroll and pagination functionality for data-driven applications. It simplifies the process of paginating large datasets by dynamically loading and rendering new data as the user scrolls.

Table of Contents

Installation

Install the package using npm or yarn:

npm i react-scroll-pagify

or

yarn add react-scroll-pagify

Basic Usage

Here's a basic example of how to use the ReactScrollPagify component:

import React, { useState } from "react";
import { ReactScrollPagify } from "react-scroll-pagify";

const YourComponent = () => {
  const [currentPage, setCurrentPage] = useState(1);
  const [data, setData] = useState([]);
  const totalPages = 50; // Calculate based on your total data

  const handlePageChange = (page) => {
    setCurrentPage(page);
    // Fetch or update your data here
  };

  const handleRefresh = () => {
    // Implement refresh logic here
  };

  return (
    <ReactScrollPagify
      data={data}
      onChangePage={handlePageChange}
      pagination={{
        page: currentPage,
        totalPage: totalPages,
      }}
      onRefresh={handleRefresh}
      enablePulling={true}
    >
      {/* You have to make component with data props. without data props it will not work */}
      <YourListComponent data={[]} />
    </ReactScrollPagify>
  );
};

Props

PropTypeDefaultDescription
childrenReact.ReactNode-The content to be rendered within the scrollable sections
thresholdnumber-The scroll threshold to trigger page changes
dataT[]-The array of data to be paginated and displayed
onChangePage(page: number) => void-Callback triggered when the page changes
onDataChange(data:T[]) => void-Callback triggered when the data changes and you can customize your data on that
onLoadMore(page: number) => void-Callback triggered when the click on load more button
pagination{ page: number; totalPage: number }-Object with current page and total pages information
isLoadingbooleanfalseIndicates if data is currently loading
enableDataMemorizationbooleantrueIndicates the given data should memorize or not. if enable it will enable the data otherswise not.
loadingOverlayReact.ReactNode-Custom loading overlay component
loadMoreButtonReact.ReactNode-Custom Load more button component
styleRootElementRecord<string, string | number | undefined>-Custom styles for the root element
rootClassNamestring-Custom class name for the root element
rootElementIdstring-Custom ID for the root element
enableLoadMoreButtonbooleanfalseEnable or disable the load more button
enablePullingbooleanfalseEnable or disable pull-to-refresh functionality
pulingOptions{ position: number | string | undefined }-Options for pull-to-refresh behavior. note: by default it will work when the root element in scrolling possition in top. you can pass any position number or "any" - it will work every places you want.
onRefresh() => void-Callback function for refresh action

Pagination

Control page management using the pagination prop:

<ReactScrollPagify
  // ...other props
  pagination={{
    page: currentPage,
    totalPage: totalPages,
  }}
>
  {/* Your content */}
</ReactScrollPagify>

Customization

Styling

Customize the scroll container:

<ReactScrollPagify
  styleRootElement={{ height: "400px", overflowY: "auto" }}
  rootClassName="custom-scrollify-container"
  rootElementId="my-scrollify-container"
>
  {/* Your content */}
</ReactScrollPagify>

Pull-to-Refresh

Enable pull-to-refresh functionality:

<ReactScrollPagify
  enablePulling={true}
  pulingOptions={{ position: 60 }}
  onRefresh={handleRefresh}
>
  {/* Your content */}
</ReactScrollPagify>

Examples

Paginating API Data

const [data, setData] = useState([]);
const [page, setPage] = useState(1);
const [totalPages, setTotalPages] = useState(1);

const fetchData = async (page) => {
  const response = await fetch(`https://api.example.com/data?page=${page}`);
  const { results, total_pages } = await response.json();
  setData((prevData) => [...prevData, ...results]);
  setTotalPages(total_pages);
};

const handlePageChange = (newPage) => {
  setPage(newPage);
  fetchData(newPage);
};

return (
  <ReactScrollPagify
    data={data}
    onChangePage={handlePageChange}
    pagination={{ page, totalPage: totalPages }}
    isLoading={isLoading}
    loadingOverlay={<div>Loading...</div>}
  >
    {/* You have to make component with data props. without data props it will not work */}
    <YourListComponent data={[]} />
  </ReactScrollPagify>
);

Custom Scrolling Behavior

<ReactScrollPagify
  threshold={100}
  enableLoadMoreButton={true}
  onChangePage={handlePageChange}
  // ...other props
>
  {/* Your content */}
  {isLastPage && <div>No more data to load</div>}
</ReactScrollPagify>

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.