0.5.3 • Published 4 months ago

react-hooks-paginator v0.5.3

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

React Hooks Paginator

Build

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.3

4 months ago

0.5.2

4 months ago

0.5.1

2 years ago

0.5.0

3 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.1

5 years ago

0.4.2

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago