Skip to content

Commit 377f519

Browse files
committed
refactor: 使用hooks重构Dashboard组件
1 parent 6ff0fae commit 377f519

File tree

1 file changed

+71
-71
lines changed

1 file changed

+71
-71
lines changed

src/views/dashboard/index.jsx

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from "react";
1+
import React, { useState } from "react";
22
import { Row, Col } from "antd";
33
import "./index.less";
44
import PanelGroup from "./components/PanelGroup";
@@ -9,7 +9,7 @@ import PieChart from "./components/PieChart";
99
import TransactionTable from "./components/TransactionTable";
1010
import BoxCard from "./components/BoxCard";
1111

12-
const lineChartData = {
12+
const lineChartDefaultData = {
1313
"New Visits": {
1414
expectedData: [100, 120, 161, 134, 105, 160, 165],
1515
actualData: [120, 82, 91, 154, 162, 140, 145],
@@ -27,77 +27,77 @@ const lineChartData = {
2727
actualData: [120, 82, 91, 154, 162, 140, 130],
2828
},
2929
};
30-
class Home extends Component {
31-
state = {
32-
lineChartData: lineChartData["New Visits"],
33-
};
3430

35-
handleSetLineChartData = (type) => {
36-
this.setState({ lineChartData: lineChartData[type] });
37-
};
38-
render() {
39-
return (
40-
<div className="app-container">
41-
<a
42-
href="https://github.com/NLRX-WJC/react-antd-admin-template"
43-
target="_blank"
44-
rel="noopener noreferrer"
45-
className="github-corner"
46-
> </a>
47-
<PanelGroup handleSetLineChartData={this.handleSetLineChartData} />
31+
const Dashboard = () => {
32+
const [lineChartData, setLineChartData] = useState(
33+
lineChartDefaultData["New Visits"]
34+
);
4835

49-
<LineChart
50-
chartData={this.state.lineChartData}
51-
styles={{
52-
padding: 12,
53-
backgroundColor: "#fff",
54-
marginBottom: "25px",
55-
}}
56-
/>
36+
const handleSetLineChartData = (type) =>
37+
setLineChartData(lineChartDefaultData[type]);
5738

58-
<Row gutter={32}>
59-
<Col xs={24} sm={24} lg={8}>
60-
<div className="chart-wrapper">
61-
<RaddarChart />
62-
</div>
63-
</Col>
64-
<Col xs={24} sm={24} lg={8}>
65-
<div className="chart-wrapper">
66-
<PieChart />
67-
</div>
68-
</Col>
69-
<Col xs={24} sm={24} lg={8}>
70-
<div className="chart-wrapper">
71-
<BarChart />
72-
</div>
73-
</Col>
74-
</Row>
39+
return (
40+
<div className="app-container">
41+
<a
42+
href="https://github.com/NLRX-WJC/react-antd-admin-template"
43+
target="_blank"
44+
rel="noopener noreferrer"
45+
className="github-corner"
46+
></a>
7547

76-
<Row gutter={8}>
77-
<Col
78-
xs={24}
79-
sm={24}
80-
md={24}
81-
lg={12}
82-
xl={12}
83-
style={{ paddingRight: "8px", marginBottom: "30px" }}
84-
>
85-
<TransactionTable />
86-
</Col>
87-
<Col
88-
xs={24}
89-
sm={24}
90-
md={24}
91-
lg={12}
92-
xl={12}
93-
style={{ marginBottom: "30px" }}
94-
>
95-
<BoxCard />
96-
</Col>
97-
</Row>
98-
</div>
99-
);
100-
}
101-
}
48+
<PanelGroup handleSetLineChartData={handleSetLineChartData} />
10249

103-
export default Home;
50+
<LineChart
51+
chartData={lineChartData}
52+
styles={{
53+
padding: 12,
54+
backgroundColor: "#fff",
55+
marginBottom: "25px",
56+
}}
57+
/>
58+
59+
<Row gutter={32}>
60+
<Col xs={24} sm={24} lg={8}>
61+
<div className="chart-wrapper">
62+
<RaddarChart />
63+
</div>
64+
</Col>
65+
<Col xs={24} sm={24} lg={8}>
66+
<div className="chart-wrapper">
67+
<PieChart />
68+
</div>
69+
</Col>
70+
<Col xs={24} sm={24} lg={8}>
71+
<div className="chart-wrapper">
72+
<BarChart />
73+
</div>
74+
</Col>
75+
</Row>
76+
77+
<Row gutter={8}>
78+
<Col
79+
xs={24}
80+
sm={24}
81+
md={24}
82+
lg={12}
83+
xl={12}
84+
style={{ paddingRight: "8px", marginBottom: "30px" }}
85+
>
86+
<TransactionTable />
87+
</Col>
88+
<Col
89+
xs={24}
90+
sm={24}
91+
md={24}
92+
lg={12}
93+
xl={12}
94+
style={{ marginBottom: "30px" }}
95+
>
96+
<BoxCard />
97+
</Col>
98+
</Row>
99+
</div>
100+
);
101+
};
102+
103+
export default Dashboard;

0 commit comments

Comments
 (0)