# 自定义hooks(抽取分页逻辑)
# hooks
hooks/useModal.js
import { useState } from 'react';
const useModal = () => {
const [isShowing, setIsShowing] = useState(false);
function toggle() {
setIsShowing(!isShowing);
}
return {
isShowing,
toggle,
}
};
export default useModal;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
hooks/usePagination.js
import { useState } from 'react';
export default () => {
const [pagination, setPagination] = useState({
pageNum: 1,
pageSize: 10
});
return {
pagination,
setPage(pageNum) {
setPagination({
pageNum,
pageSize: pagination.pageSize
});
},
setPageSize(pageSize) {
setPagination({
pageNum:1,
pageSize
});
}
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
hooks/useTableDataLoader.js
import { useState, useEffect } from 'react';
export default (api, pagination) => {
const [data, setData] = useState({
total: 0,
tableData: []
});
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true);
api(pagination)
.then(res => {
setData({
total: res.data.totalCount,
tableData: res.data.list
});
})
.catch(error => {
console.log(error)
})
.finally(() => {
setLoading(false);
});
}, [pagination]);
return {
data, loading
};
}
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
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
# components
components/TableDisplay.jsx
import React from 'react';
import { Table } from 'antd';
export default (props) => {
const {loading = fasle,data = [], columns = [] } = props
return (
<Table loading={loading} dataSource={data} pagination={false} columns={columns} ></Table>
)
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
components/Pagination.jsx
import React from 'react';
import { Pagination } from 'antd';
export default React.memo((props) => {
const { total = 0, setPage, setPageSize, pageNum: page, pageSize } = props;
return(
<div style={{paddingTop:'20px'}}>
<Pagination
current={page}
onChange={(page) => {setPage(page)}}
onShowSizeChange={(page,pageSize) => setPageSize(pageSize)}
showSizeChanger
total={total}
pageSize={pageSize}
> </Pagination>
</div>
)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
index.jsx
import React, { Component } from 'react';
import { getParkingSpaceRealtimelistApi } from '@/services/parkingStatistics/realTimeParkingData';
import { Table, Modal } from 'antd';
import TableDisplay from '@/components/TableDisplay';
import Pagination from '@/components/Pagination';
import useTableDataLoader from '@/hooks/useTableDataLoader';
import usePagination from '@/hooks/usePagination';
import useModal from '@/hooks/useModal';
import { Button } from 'antd';
export default () => {
const columns = [
{
title: '名称',
dataIndex: 'name',
key: 'name',
},
{
title: '区域',
dataIndex: 'region',
key: 'region',
},
{
title: '类型',
dataIndex: 'type',
key: 'type',
},
{
title: '操作',
render(record) {
return <Button onClick={toggle}>编辑</Button>
}
}
];
const { pagination, setPage, setPageSize } = usePagination();
const { isShowing,toggle } = useModal();
const { data, loading } = useTableDataLoader(getParkingSpaceRealtimelistApi, pagination);
const { tableData, total } = data;
return (
<div>
<TableDisplay {...{ data: tableData, loading, columns }}></TableDisplay>
<Pagination
{
...{
total:total,
setPage,
setPageSize,
...pagination
}
}
></Pagination>
<Modal
visible={isShowing}
title={'23'}
onCancel={() => toggle()}
>
hello
</Modal>
</div>
);
}
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67