-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusePagination.ts
40 lines (34 loc) · 997 Bytes
/
usePagination.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { useEffect, useState } from "react";
type Seed = (totalPages: number, rowStart: number) => void;
type OnChange = (_: any, page: number) => void;
type UsePagination = {
count: number;
page: number;
seed: Seed;
onChange: OnChange;
rowStart: number;
};
const usePagination = (): UsePagination => {
const [count, setCount] = useState<number>(0);
const [page, setPage] = useState(() => {
const page = sessionStorage.getItem("page");
if (page) return parseInt(page);
return 1;
});
const [rowStart, setRowStart] = useState(1);
const seed: Seed = (totalPages: number, rowStart: number = 1) => {
setCount(totalPages);
setRowStart(rowStart);
};
useEffect(() => {
return () => {
sessionStorage.removeItem("page");
};
}, []);
const onChange = (_: any, page: number) => {
setPage(page);
sessionStorage.setItem("page", page.toString());
};
return { count, page, seed, onChange, rowStart };
};
export default usePagination;