1.6.8 • Published 2 years ago

react-custom-pagination v1.6.8

Weekly downloads
34
License
ISC
Repository
github
Last release
2 years ago

react-custom-pagination

simple and responsive pagination for react.

NPM JavaScript Style Guide

Install

npm install --save react-custom-pagination

what is this ?

Get Responsive and yet simple pagination for React .

installation

npm install react-custom-pagination --save

import

import Pagination from "react-custom-pagination";

pagination

usage

 //list of items you want to render

 const posts=[
    { id: "1", name: "user1" },
    { id: "2", name: "user2" },
    { id: "3", name: "user3" },
    { id: "4", name: "user4" },
    { id: "5", name: "user5" },
    { id: "6", name: "user6" },
    ]


  const [currentPage, setCurrentPage] = useState(1);        //page
  const [postsPerPage] = useState(5);                      // no of posts you want to render in one page

  //get current Posts

  const indexOfLastPost = currentPage * postsPerPage;
  const indexOfFirstPost = indexOfLastPost - postsPerPage;
  const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);

  // when user clicks on number this function will execute

  const paginate = (number) => {
    setCurrentPage(number);
  };


//map current posts to render 5 posts per page

<Pagination
  totalPosts={posts.length}
  postsPerPage={postsPerPage}
  paginate={paginate}
/>

example

import React, { useState } from "react";
import "./App.css";
import Pagination from "react-custom-pagination";

const App = () => {
  const posts = [
    { id: "1", name: "user 1" },
    { id: "2", name: "user 2" },
    { id: "3", name: "user 3" },
    { id: "4", name: "user 4" },
    { id: "5", name: "user 5" },
    { id: "6", name: "user 6" },
    { id: "7", name: "user 7" },
    { id: "8", name: "user 8" },
    { id: "9", name: "user 9" },
    { id: "10", name: "user 10" },
    { id: "11", name: "user 11" },
    { id: "12", name: "user 12" },
    { id: "13", name: "user 13" },
    { id: "14", name: "user 14" },
    { id: "15", name: "user 15" },
    { id: "16", name: "user 16" },
    { id: "17", name: "user 17" },
    { id: "18", name: "user 18" },
    { id: "19", name: "user 19" },
    { id: "20", name: "user 20" },
  ];

  const [currentPage, setCurrentPage] = useState(1);
  const [postsPerPage] = useState(3);

  //get current Posts

  const indexOfLastPost = currentPage * postsPerPage;
  const indexOfFirstPost = indexOfLastPost - postsPerPage;
  const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);

  // when user clicks on number this function will execute

  const paginate = (number) => {
    setCurrentPage(number);
  };

  return (
    <>
      <div
        style={{
          width: "500px",
          textAlign: "center",
          marginTop: "50px",
          height: "200px",
        }}
      >
        <h1 style={{ textAlign: "center", color: "green" }}>
          Pagination Example
        </h1>
        {currentPosts.map((item) => {
          return (
            <p key={item.id} style={{ color: "red" }}>
              {item.name}
            </p>
          );
        })}
      </div>
      <div style={{ width: "500px" }}>
        <Pagination
          totalPosts={posts.length}
          postsPerPage={postsPerPage}
          paginate={paginate}
          view={5}
          showLast={true}
          showFirst={true}
          showIndex={true}
        />
      </div>
    </>
  );
};
export default App;

Required

  1. totalPosts (required) //total no of posts
 <Pagination
    totalPosts={posts.length}
  />
  1. postsPerPage (required) // no of posts per page
 <Pagination
   postsPerPage={postsPerPage}
  />
  1. paginate (required)

// when user clicks on number this function will execute and change currentPage

//paginate function

 const paginate = (number) => {
    setCurrentPage(number);
  };

//pagination component

<Pagination
    paginate={paginate}
 />

props

NameRequiredTypeDefaultDescription
totalPostsYesNumber---total no of posts
postsPerPageYesNumber---no of posts per page
paginateYesfunction---this function will execute when click on paginate number
viewNoNumber5no of paginates
showIndexNoBooleanfalseshows the index bar
showFirstNoBooleanfalseshows the go to first page button
showLastNoBooleanfalseshows the go to Last page button
showFirstTextNoString"First Page"text inside show first page button
showLastTextNoString"Last Page"text inside show last page button
colorNoString"white"color of the text
bgColorNoString"skyblue"background color
indexbgColorNoString"tomato"background color of index bar
selectColorNoString"grey"background color when selected
boxHeightNoString,Number"40px"height
boxWidthNoString,Number"40px"width
boxRadiusNoString"50%"border-radius
indexBorderRadiusNoString"5%"border-radius of index tab
justifyNoString"center"justify-content

optional props

//view
<Pagination
    view={5}           //no of paginates
 />

License

ISC © akhilv77

1.6.4

2 years ago

1.5.5

2 years ago

1.4.6

2 years ago

1.3.7

2 years ago

1.4.5

2 years ago

1.6.2

2 years ago

1.4.4

2 years ago

1.6.1

2 years ago

1.5.2

2 years ago

1.4.3

2 years ago

1.6.0

2 years ago

1.5.1

2 years ago

1.4.2

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.6.8

2 years ago

1.5.9

2 years ago

1.5.8

2 years ago

1.4.9

2 years ago

1.6.6

2 years ago

1.5.7

2 years ago

1.4.8

2 years ago

1.3.9

2 years ago

1.6.5

2 years ago

1.5.6

2 years ago

1.4.7

2 years ago

1.3.8

2 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.3.1

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago