Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Custom usePagination hook

interface UsePaginationProps {
    contentPerPage: number,
    count: number,
}

interface UsePaginationReturn {
    page: number;
    totalPages: number;
    firstContentIndex: number;
    lastContentIndex: number;
    nextPage: () => void;
    prevPage: () => void;
    setPage: (page: number) => void;
}

type UsePagination = (UsePaginationProps) => (UsePaginationReturn);
Comment

Custom usePagination hook example

import { useState } from "react";

const usePagination: UsePagination = ({ contentPerPage, count }) => {
  const [page, setPage] = useState(1);
  // number of pages in total (total items / content on each page)
  const pageCount = Math.ceil(count / contentPerPage);
  // index of last item of current page
  const lastContentIndex = page * contentPerPage;
  // index of first item of current page
  const firstContentIndex = lastContentIndex - contentPerPage;

  // change page based on direction either front or back
  const changePage = (direction: boolean) => {
    setPage((state) => {
      // move forward
      if (direction) {
        // if page is the last page, do nothing
        if (state === pageCount) {
          return state;
        }
        return state + 1;
        // go back
      } else {
        // if page is the first page, do nothing
        if (state === 1) {
          return state;
        }
        return state - 1;
      }
    });
  };

  const setPageSAFE = (num: number) => {
    // if number is greater than number of pages, set to last page
    if (num > pageCount) {
      setPage(pageCount);
      // if number is less than 1, set page to first page
    } else if (num < 1) {
      setPage(1);
    } else {
      setPage(num);
    }
  };

  return {
    totalPages: pageCount,
    nextPage: () => changePage(true),
    prevPage: () => changePage(false),
    setPage: setPageSAFE,
    firstContentIndex,
    lastContentIndex,
    page,
  };
};

export default usePagination;
Comment

PREVIOUS NEXT
Code Example
Javascript :: Renaming props in react 
Javascript :: Example code of using inner blocks in Wordpress with ESNext 
Javascript :: adding values for metaboxes in wordpress 
Javascript :: Key and property shorthand in ES6 
Javascript :: on click disable esc button using jquery 
Javascript :: copy file using java script 
Javascript :: pass props to svg 
Javascript :: createElement calls without JSX 
Javascript :: check string length pixel "react" 
Javascript :: merge json data in main.go in golang 
Javascript :: tampermonkey all pages 
Javascript :: change user agent in playwright 
Javascript :: filter array and get index of num 
Javascript :: date javascript only show day month year 
Javascript :: make a circle in javascript 
Javascript :: group by hash in jquery 
Javascript :: preventdefault called two times 
Javascript :: Javascript - The file size is measured in bytes 
Javascript :: underscore js shuffle 
Javascript :: create elements 
Javascript :: { "rules": { ".read": true, ".write": true } } 
Javascript :: spring reactive web client throw exception test 
Javascript :: mdn spread 
Javascript :: error code ELIFECYCLE REACTJs 
Javascript :: khai bao bien trong javascript 
Javascript :: how to like posts on instagram js 
Javascript :: knex update and list all record mysql 
Javascript :: how to restrict page leave in javascript 
Javascript :: firebase is there a way to rename a document 
Javascript :: discord.js send dm to specific user 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =