use-pagination.ts


Overview

The use-pagination.ts file provides a custom React hook named useClientPagination that facilitates client-side pagination of data arrays. It enables React components to easily manage the current page, page size, and paginated subset of a larger list without requiring server-side pagination logic.

This hook is particularly useful in user interfaces where a large dataset is retrieved once and needs to be displayed in smaller chunks or pages, improving readability and performance by limiting the number of items rendered at a time.


Detailed Description

useClientPagination(list: Array<any>)

A React hook that manages pagination state and logic for a given array of data.

Parameters

Returns

An object containing the following properties and functions:

Property / Function

Type

Description

page

number

The current page number (1-based index).

pageSize

number

The number of items per page. Defaults to 10 initially.

setPage

(page: number) => void

Setter function to update the current page number.

setPageSize

(pageSize: number) => void

Setter function to update the page size (items per page).

onPaginationChange

(page: number, pageSize: number) => void

Callback function to update both page and page size simultaneously.

pagedList

Array<any>

The subset of the original list corresponding to the current page and page size.

Usage Example

import React from 'react';
import { useClientPagination } from './use-pagination';

const MyPaginatedComponent = ({ data }) => {
  const {
    page,
    pageSize,
    setPage,
    setPageSize,
    onPaginationChange,
    pagedList,
  } = useClientPagination(data);

  return (
    <div>
      <ul>
        {pagedList.map(item => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
      <PaginationControls
        currentPage={page}
        pageSize={pageSize}
        totalItems={data.length}
        onChange={onPaginationChange}
      />
    </div>
  );
};

Implementation Details


Interaction with Other System Parts


Visual Diagram

flowchart TD
    A[useClientPagination(list)] --> B[useState: page (default 1)]
    A --> C[useState: pageSize (default 10)]
    A --> D[useCallback: onPaginationChange(page, pageSize)]
    A --> E[useMemo: pagedList = list.slice((page-1)*pageSize, page*pageSize)]
    B --> F[setPage]
    C --> G[setPageSize]
    A --> H[returns { page, pageSize, setPage, setPageSize, onPaginationChange, pagedList }]

Summary

use-pagination.ts provides a simple yet effective custom React hook for managing client-side pagination of any array of data. It abstracts state management and slicing logic, allowing seamless integration into React components that require paginated views. The use of React hooks (useState, useCallback, useMemo) ensures performance optimization and clean handling of pagination updates.

This utility is focused purely on the logic layer and relies on external UI components to render controls and display paginated data. It supports flexible page sizes and page navigation for dynamic user interaction.