0.3.3 • Published 5 years ago

@itsfaqih/usepagination v0.3.3

Weekly downloads
151
License
MIT
Repository
github
Last release
5 years ago

usePagination

Simple client side pagination hooks for react.

NPM

Install

npm install --save @itsfaqih/usepagination

Usage

import React from 'react';
import Post from 'component/post';
import Pagination from 'component/pagination';
import usePagination from '@itsfaqih/usepagination';

const Home = () => {
  const posts = [
    {
      id: 1,
      title: 'Nice hooks',
      body: 'You can also use data from an API. As long as it returns an array',
    },
    ...
  ];

  const { page, action } = usePagination(posts);

  return (
    <div>
      <h1>Posts</h1>
      <div>
        {page.data.map(item => <Post.Card data={item}>)}
      </div>
      <div>
        <Pagination>
          <Pagination.First
            onClick={() => action.goTo(1)}
            disabled={page.current === 1}
          />
          <Pagination.Previous
            onClick={action.previous}
            disabled={page.previous === null}
          />
          {page.numbers.map((number, index) => (
            <Pagination.Number
              key={index}
              number={number}
              onClick={() => action.goTo(number)}
              disabled={page.current === number}
            />
          ))}
          <Pagination.Next
            onClick={action.next}
            disabled={page.next === null}
          />
          <Pagination.Last
            onClick={() => action.goTo(page.last)}
            disabled={page.current === page.last}
          />
        </Pagination>
      </div>
    </div>
  );
};

API

const { page, action } = usePagination(initialData, perPage, shownPageNumber);

parameters

ParameterTypeDescription
initialDataArrayThe array of the data to be paginated
perPagenumberNumber of data to be shown per page
shownPageNumbernumberAmount of number to be shown in pagination buttons

page

PropertyTypeDescription
dataArrayData of current active page
numbersArrayGenerated page numbers for pagination buttons
currentnumber | nullCurrent active page number. It will be null on empty initialData
nextnumber | nullNext page number. It will be null on empty initialData or no more next page
previousnumber | nullPrevious page number. It will be null on empty initialData or active first page
lastnumberLast page number. It will be 0 on empty initialData

action

PropertyTypeArgumentDescription
nextFunction-Change to the next page
previousFunction-Change to the previous page
goToFunctionnumberChange to specified page

License

MIT © itsfaqih


This hook is created using create-react-hook.

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago