/
pasha.json
/
react_pro_final_project_base
Обзор
Документация
Войти
/
pasha.json
/
react_pro_final_project_base
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
finalTask
src/shared/hooks/usePagination.ts
35 строк
859 B
Pavel Nikolaev
tasks 1-5
19 ноя 2025, 17:07
19 ноя 2025, 17:07
804edd8
Код
Авторство
О чём код?
import { useState } from 'react'; export const usePagination = <T>(data: T[], itemsPerPage: number) => { const [currentPage, setCurrentPage] = useState<number>(1); const countPages = Math.ceil(data.length / itemsPerPage); const MIN_PAGE = 1; function getCurrentData() { const start = (currentPage - 1) * itemsPerPage; const end = start + itemsPerPage; return data.slice(start, end); } function nextPage() { setCurrentPage((currentPage) => Math.min(currentPage + 1, countPages)); } function prevPage() { setCurrentPage((currentPage) => Math.max(currentPage - 1, MIN_PAGE)); } function setPagePaginated(page: number) { const pageNumber = Math.max(MIN_PAGE, page); setCurrentPage(() => Math.min(pageNumber, countPages)); } return { getCurrentData, countPages, currentPage, nextPage, prevPage, setPagePaginated, }; };