0.5.1 • Published 1 year ago

react-hooks-paginator v0.5.1

Weekly downloads
1,452
License
MIT
Repository
github
Last release
1 year ago

React Hooks Paginator

Build Status

demo

A library for adding simple paginator functionality to your react app.

Requires react >= 16.8.0

Installation

Run the following command: npm install react-hooks-paginator

Usage

import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import Paginator from 'react-hooks-paginator';

function App() {
  const [offset, setOffset] = useState(0);
  const [currentPage, setCurrentPage] = useState(1);

  const data = [page1, page2, page3, page4, page5, page6];

  return (
    <div>
      <Paginator
        totalRecords={data.length}
        pageLimit={4}
        pageNeighbours={1}
        setOffset={setOffset}
        currentPage={currentPage}
        setCurrentPage={setCurrentPage}
      />
    </div>
  );
}

export default App;

Props

PropertyTypeDefaultDescription
totalRecordsNumber (required)The length of the data we are tabulating
pageLimitNumber (required)Minimum number of records per page
pageNeighboursNumber (required)Number of page blocks by the left and right of the middle page
setOffsetfunction (required)function that updates the offset state
setCurrentPagefunction (required)function that updates the current page state
currentPageNumber (required)The current page state
pageContainerClassStringreact-hooks-paginatorPaginator container classname
pageActiveClassStringactiveActive page item classname
pageItemClassStringpage-itemPage item classname
pageLinkClassStringpage-linkPage link classname
pagePrevTextString or NodeNext »Prev page item text
pageNextTextString or Node« PrevNext page item text
pagePrevClassStringpage-linkPrev page custom class
pageNextClassStringnext-linkNext page custom class

Example

import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
import Paginator from 'react-hooks-paginator';
import { fetchData } from './data-fetcher';

function App() {
  const pageLimit = 10;

  const [offset, setOffset] = useState(0);
  const [currentPage, setCurrentPage] = useState(1);
  const [data, setData] = useState([]);
  const [currentData, setCurrentData] = useState([]);

  useEffect(() => {
    fetchData().then(data => setData(data));
  }, []);

  useEffect(() => {
    setCurrentData(data.slice(offset, offset + pageLimit));
  }, [offset, data]);

  return (
    <div>
      <ul>
        {currentData.map(data => (
          <li>{data}</li>
        ))}
      </ul>
      <Paginator
        totalRecords={data.length}
        pageLimit={pageLimit}
        pageNeighbours={2}
        setOffset={setOffset}
        currentPage={currentPage}
        setCurrentPage={setCurrentPage}
      />
    </div>
  );
}

export default App;
0.5.1

1 year ago

0.5.0

3 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.1

4 years ago

0.4.2

4 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago