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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sonardigital/pagination

v1.0.8

Published

Lightweight and customizable pagination component for React applications.

Downloads

7

Readme

@sonardigital/pagination

Lightweight and customizable pagination component for React applications with built-in URL synchronization support.

Features

  • 🎯 React Hook: Simple usePagination hook for state management
  • 🔗 URL Sync: Optional URL parameter synchronization
  • 🎨 Provider Pattern: Context-based state sharing across components
  • Navigation Controls: next, prev, jump, and reset functions
  • 🛡️ Type Safety: Full TypeScript support
  • 🔧 Flexible: Works with or without React Router

Installation

npm install @sonardigital/pagination

Peer Dependencies

This package requires the following peer dependencies:

{
  "react": "*",
  "react-dom": "*",
  "react-router": "*"
}

Quick Start

Basic Usage (Without URL Sync)

import { usePagination } from '@sonardigital/pagination';

function MyComponent() {
  const pagination = usePagination({
    page: 1,
    limit: 25,
    total: 100,
    onPageChange: ({ page, limit }) => {
      console.log(`Page: ${page}, Limit: ${limit}`);
      // Fetch data here
    }
  });

  return (
    <div>
      <button onClick={pagination.prev} disabled={pagination.disabledPrev}>
        Previous
      </button>
      <span>Page {pagination.page} of {pagination.totalPages}</span>
      <button onClick={pagination.next} disabled={pagination.disabledNext}>
        Next
      </button>
    </div>
  );
}

With URL Synchronization

import { usePagination } from '@sonardigital/pagination';

function MyComponent() {
  const pagination = usePagination({
    page: 1,
    limit: 25,
    total: 100,
    syncWithUrl: true,
    onPageChange: ({ page, limit }) => {
      // Fetch data when page/limit changes
      fetchData(page, limit);
    }
  });

  // URL will automatically update: ?page=2&limit=25
  return (
    <div>
      <button onClick={pagination.prev} disabled={pagination.disabledPrev}>
        Previous
      </button>
      <span>Page {pagination.page} of {pagination.totalPages}</span>
      <button onClick={pagination.next} disabled={pagination.disabledNext}>
        Next
      </button>
    </div>
  );
}

Using Provider Pattern

import { usePagination, PaginationProvider, usePaginationContext } from '@sonardigital/pagination';

function App() {
  const paginationMethods = usePagination({
    page: 1,
    limit: 25,
    total: 100,
    syncWithUrl: true
  });

  return (
    <PaginationProvider methods={paginationMethods}>
      <DataList />
      <PaginationControls />
    </PaginationProvider>
  );
}

function PaginationControls() {
  const pagination = usePaginationContext();

  return (
    <div>
      <button onClick={pagination.prev} disabled={pagination.disabledPrev}>
        Previous
      </button>
      <span>Page {pagination.page} of {pagination.totalPages}</span>
      <button onClick={pagination.next} disabled={pagination.disabledNext}>
        Next
      </button>
    </div>
  );
}

API Reference

usePagination(props)

Hook for managing pagination state.

Props

interface UsePaginationProps {
  page?: number;           // Initial page (default: 1)
  limit?: number;          // Items per page (default: 25)
  total?: number;          // Total number of items (default: 0)
  onPageChange?: (params: { page: number; limit: number }) => void;
  syncWithUrl?: boolean;   // Enable URL synchronization (default: false)
}

Returns

interface PaginationContextType {
  // State
  page: number;
  limit: number;
  totalItems: number;
  totalPages: number;
  
  // Navigation
  next: () => void;
  prev: () => void;
  jump: (pageNum: number) => void;
  reset: () => void;
  
  // Configuration
  setLimit: (limit: number) => void;
  setTotal: (total: number) => void;
  
  // Status
  disabledNext: boolean;
  disabledPrev: boolean;
  
  // Utilities
  queryParams: string;  // e.g., "page=1&limit=25"
}

PaginationProvider

Context provider for sharing pagination state across components.

interface PaginationProviderProps {
  methods: ReturnType<typeof usePagination>;
  children: React.ReactNode | React.ReactNode[];
}

usePaginationContext()

Hook to access pagination context within a PaginationProvider.

Advanced Examples

Changing Items Per Page

const pagination = usePagination({
  page: 1,
  limit: 25,
  total: 100
});

// Change limit (automatically resets to page 1)
pagination.setLimit(50);

Jumping to Specific Page

const pagination = usePagination({
  page: 1,
  limit: 25,
  total: 100
});

// Jump to page 5
pagination.jump(5);

Using Query Params

const pagination = usePagination({
  page: 1,
  limit: 25,
  total: 100
});

// Get formatted query string
const queryString = pagination.queryParams; // "page=1&limit=25"

// Use in API calls
fetch(`/api/items?${queryString}`);

Utilities

The package also exports utility functions for custom implementations:

paginationCalculations

import { paginationCalculations } from '@sonardigital/pagination';

paginationCalculations.calculateTotalPages(totalItems, limit);
paginationCalculations.isNextDisabled(currentPage, totalPages);
paginationCalculations.isPrevDisabled(currentPage);
paginationCalculations.isValidPage(pageNum, maxPages);

urlParams

import { urlParams } from '@sonardigital/pagination';

urlParams.parseUrlParam(value, defaultValue);
urlParams.getPaginationFromUrl(searchParams, defaultPage, defaultLimit);
urlParams.buildPaginationUrl(pathname, searchParams, page, limit);

Notes

  • When syncWithUrl is enabled, pagination state syncs with URL query parameters
  • Changing the limit automatically resets to page 1
  • The hook handles browser back/forward navigation when URL sync is enabled
  • Navigating to a new route resets pagination to initial values

License

ISC