1.0.7-b • Published 3 months ago

react-use-paging v1.0.7-b

Weekly downloads
88
License
MIT
Repository
github
Last release
3 months ago

react-use-paging

NPM JavaScript Style Guide

demo

Quick and easy react hook to implement pagination as a list.

Install

npm install --save react-use-paging

Usage

import React from 'react';
import usePagination from 'react-use-paging';

const MyComponent = () => {
     const myArrayOfThings = [
        { id: 1, title: 'first' },
        { id: 2, title: 'second' },
        ...
    ];
    const { items, pages, setCurrentPage, currentPage } = usePagination(
        myArrayOfThings,
        {
            resultPerPage: 2,
            alwaysVisible: false // First page won't show if all results are in one page.
        }
    );
    return (
        <>
            {items.map(item => (
                <div>
                    <div className="row" key={item.id}>
                        {item.title}
                    </div>
                </div>
            ))}
            {pages.map(page => (
                <button
                    key={page}

                    // You add style to the current page by cheking the current page you are on.
                    className={page === currentPage ? 'my-active-classs' : ''}
                    onClick={() => setCurrentPage(page)}
                >
                    page {page}
                </button>
            ))}
        </>
    );
};

Options

NameTypeDefault valueIs RequiredDescription
... Array YesContent you want to be paginated.
{ ... }Object{}YesOptions, can be empty but required.
resultPerPageNumber2NoNumber of result per page.
alwaysVisibleBooleantrueNoif false first page won't show if all results are shown in one page.

License

MIT


This hook is created using create-react-hook.

1.0.7-b

3 months ago

1.0.7-a

3 months ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago