Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

pagination hook react

1  import React, { useState } from "react";
2
3  function usePagination(data, itemsPerPage) {
4    const [currentPage, setCurrentPage] = useState(1);
5    const maxPage = Math.ceil(data.length / itemsPerPage);
6
7  function currentData() {
8    const begin = (currentPage - 1) * itemsPerPage;
9    const end = begin + itemsPerPage;
10   return data.slice(begin, end);
11  }
12
13  function next() {
14    setCurrentPage((currentPage) => Math.min(currentPage + 1, maxPage));
15  }
16
17  function prev() {
18    setCurrentPage((currentPage) => Math.max(currentPage - 1, 1));
19  }
20
21  function jump(page) {
22    const pageNumber = Math.max(1, page);
23    setCurrentPage((currentPage) => Math.min(pageNumber, maxPage));
24  }
25
26  return { next, prev, jump, currentData, currentPage, maxPage };
27 }
28
29 export default usePagination;
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to add button react native app.js 
Javascript :: javascript check if variable is number 
Javascript :: use static pages nodejs 
Javascript :: express return json 
Javascript :: .sort javascript 
Javascript :: How to use useState Hook in React/ReactNative 
Javascript :: create module in js 
Javascript :: a function that calls itself js 
Javascript :: regex match line that does not contain string 
Javascript :: jquery get meta value 
Javascript :: remove an element from an array javascript 
Javascript :: async react setstate 
Javascript :: copy to clipboard javascript dom 
Javascript :: how to change image source using javascript 
Javascript :: window onscroll position fixed position in jquery 
Javascript :: .ignore file nodejs 
Javascript :: can i pass data with usenavigate react router 
Javascript :: for loop on a array 
Javascript :: how to tell the javascript to wait until the site loads in the html 
Javascript :: momentTimeZone 
Javascript :: access key of object javascript 
Javascript :: javascript find the min in array of numbers 
Javascript :: javascript add 1 day to new date 
Javascript :: clear file upload jquery 
Javascript :: document onload 
Javascript :: type of javascript 
Javascript :: javascript object tostring 
Javascript :: predicate function javascript 
Javascript :: mac node change version 16 
Javascript :: url regex javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =