picpagination.tsx 1.83 KB
Newer Older
백승민's avatar
1    
백승민 committed
1
import React, { useRef } from "react";
백승민's avatar
백승민 committed
2
3

type num = {
백승민's avatar
백승민 committed
4
    total: number,
백승민's avatar
1    
백승민 committed
5
6
    page: number,
    setPage: Function
백승민's avatar
백승민 committed
7
}
백승민's avatar
1    
백승민 committed
8
9

export function Pagination({ total, page, setPage }: num) {
백승민's avatar
백승민 committed
10
    const numPages = Math.ceil(total / 15);
백승민's avatar
1    
백승민 committed
11
12
13
14
15
16
17
18
19
20
21
    const firstLeftClick = useRef(true);
    const firstRightClick = useRef(true);

    const left = () => {
        if (firstLeftClick.current) {
            firstLeftClick.current = false;
            firstRightClick.current = true;
        } else {
            setPage(page - 1)
        }
    };
백승민's avatar
백승민 committed
22

백승민's avatar
1    
백승민 committed
23
24
25
26
27
28
29
    const right = () => {
        if (firstRightClick.current) {
            firstLeftClick.current = true;
            firstRightClick.current = false;
        } else {
            setPage(page + 1)
        }
백승민's avatar
백승민 committed
30
31
    };

백승민's avatar
백승민 committed
32
33
    return (
        <div>
백승민's avatar
백승민 committed
34
35
            <button onClick={left} disabled={page === 1}>
                &lt;{page}</button>
백승민's avatar
백승민 committed
36

백승민's avatar
백승민 committed
37
            {/* {Array(numPages)
백승민's avatar
백승민 committed
38
39
40
41
42
                .fill(1)
                .map((_, i) => (
                    <button key={i + 1} onClick={() => setPage(i + 1)}>
                        {i + 1}
                    </button>
백승민's avatar
백승민 committed
43
                ))} */}
백승민's avatar
백승민 committed
44
45

            <button onClick={right} disabled={page === numPages}>
백승민's avatar
백승민 committed
46
47
                &gt;
            </button>
백승민's avatar
1    
백승민 committed
48

백승민's avatar
백승민 committed
49
50
51
        </div>
    );
};
백승민's avatar
1    
백승민 committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// export function PaginationRight({ total, page, setPage }: num) {
//     const numPages = Math.ceil(total / 15);
//     const firstLeftClick = useRef(true);
//     const firstRightClick = useRef(true);


//     const right = () => {
//         if (firstRightClick.current) {
//             firstLeftClick.current = true;
//             firstRightClick.current = false;
//         } else {
//             setPage(page + 1)
//         }
//     };

//     return (
//         <div>
//             <button onClick={right} disabled={page === numPages}>
//                 &gt;
//             </button>
//         </div>
//     );
// };