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 :: Pass Props to a Component Using Short circuit evaluation in react 
Javascript :: Example code of using inner blocks in Wordpress with ES5 
Javascript :: Register post meta of sidebar in wordpress 
Javascript :: Implicit returns in ES6 
Javascript :: highlight row javascript 
Javascript :: js hide modal event listener name 
Javascript :: prevent the fire of parent event listener 
Javascript :: image opacity reduce js 
Javascript :: svg documentation 
Javascript :: image support in node js chat app 
Javascript :: Getting Specific Element Properties 
Javascript :: close worker 
Javascript :: change color jquery css 
Javascript :: icon with label in react native 
Javascript :: why is table.current.row.length not working 
Javascript :: Insert tag in XML text for mixed words 
Javascript :: for getting options id using javascript 
Javascript :: getting ad to close jquery 
Javascript :: linked list distance between two nodes 
Javascript :: Send data (pass message) from a (non content script ) extension component to the content script 
Javascript :: NodeJS: Good way to write Multiple API Calls in serial 
Javascript :: discord javascript error on startup 
Javascript :: contoh penggunaan promise 
Javascript :: browser app get screen siwe 
Javascript :: khai bao bien js 
Javascript :: geocoding react 
Javascript :: how to refresh a page in javascript 
Javascript :: https://www.npmjs.com/package/ngx-lightbox 
Javascript :: dummy servers using nodejs 
Javascript :: How to show content-type:image/jpg in react 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =