Skip to content

Commit 808c803

Browse files
author
wb-lp356237
committed
hooks
1 parent 97e5ee9 commit 808c803

File tree

15 files changed

+201
-135
lines changed

15 files changed

+201
-135
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"no-lonely-if": 0, // 允许有单个if
2121
"no-param-reassign": 0, // 允许修改函数参数
2222
"import/prefer-default-export": 0,
23-
"indent": [1, 2] // 缩进
23+
"indent": [1, 2], // 缩进
24+
"no-plusplus": 0,
25+
"react/prop-types": 0
2426
}
2527
}

mocks/db.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"data":[
33
{
44
"key": 1,
5-
"name": "胡彦斌",
6-
"age": 32,
7-
"address": "西湖区湖底公园1号"
5+
"name": "张三丰",
6+
"age": 100,
7+
"address": "武当山"
88
},
99
{
1010
"key": 2,
11-
"name": "胡彦祖",
12-
"age": 42,
13-
"address": "西湖区湖底公园1号"
11+
"name": "张无忌",
12+
"age": 20,
13+
"address": "光明顶"
1414
}
1515
]
1616
}

src/App.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import React from "react";
22
import { BrowserRouter as Router, Switch, Route, Redirect } from "react-router-dom";
3-
// import Layout from "../src/components/Layout";
3+
import Layout from "../src/components/Layout";
44
import Dashboard from "./pages/dashboard";
55
import About from "./pages/about";
6-
import Test from "./pages/test/A";
76
import "./App.css";
87

98
const App: React.FC = () => {
109
return (
1110
<Router>
12-
{/* <Layout> */}
13-
<Switch>
14-
<Redirect exact from="/" to="/Test" />,
15-
<Route path="/dashboard" component={Dashboard} />
16-
<Route path="/about" component={About} />
17-
<Route path="/test" component={Test} />
18-
</Switch>
19-
{/* </Layout> */}
11+
<Layout>
12+
<Switch>
13+
<Redirect exact from="/" to="/dashboard" />,
14+
<Route path="/dashboard" component={Dashboard} />
15+
<Route path="/about" component={About} />
16+
</Switch>
17+
</Layout>
2018
</Router>
2119
);
2220
};

src/components/Layout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const LayoutWrap = (props: Props) => {
4141
borderBottom: "1px solid #eee"
4242
}}
4343
>
44-
LENS后台管理系统
44+
LENS
4545
</Header>
4646
<Content
4747
style={{

src/hooks/demo/useEffect.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-disable no-plusplus */
2+
import React from "react";
3+
import ReactDOM from "react-dom";
4+
import App from "../../App";
5+
6+
const allDeps: any[][] = [];
7+
let effectCursor = 0;
8+
9+
export function useEffect(callback: () => void, deps: any[]) {
10+
if (!allDeps[effectCursor]) {
11+
// 初次渲染:赋值 + 调用回调函数
12+
allDeps[effectCursor] = deps;
13+
++effectCursor;
14+
setTimeout(() => {
15+
callback();
16+
});
17+
return;
18+
}
19+
20+
const currenEffectCursor = effectCursor;
21+
const rawDeps = allDeps[currenEffectCursor];
22+
// 检测依赖项是否发生变化,发生变化需要重新render
23+
const isChanged = rawDeps.some((dep: any, index: number) => dep !== deps[index]);
24+
if (isChanged) {
25+
callback();
26+
allDeps[effectCursor] = deps;
27+
render();
28+
}
29+
++effectCursor;
30+
}
31+
32+
const render = () => {
33+
ReactDOM.render(<App />, document.getElementById("root"));
34+
effectCursor = 0;
35+
};

src/hooks/demo/useState.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* eslint-disable no-plusplus */
2+
import React from "react";
3+
import ReactDOM from "react-dom";
4+
import App from "../../App";
5+
6+
const _state: any[] = [];
7+
let cursor = 0;
8+
9+
function useState<T>(initialState: T): [T, (value: T) => void] {
10+
_state[cursor] = _state[cursor] || initialState;
11+
const currentCursor = cursor;
12+
function setState(newState: T) {
13+
_state[currentCursor] = newState;
14+
// 重新渲染
15+
render();
16+
}
17+
return [_state[cursor++], setState];
18+
}
19+
20+
const render = () => {
21+
ReactDOM.render(<App />, document.getElementById("root"));
22+
cursor = 0;
23+
};
24+
25+
export default useState;

src/hooks/useModal.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useCallback, useState } from "react";
2+
3+
const useModal = () => {
4+
const [visible, setVisible] = useState<boolean>(false);
5+
6+
const hide = useCallback(() => {
7+
setVisible(false);
8+
}, []);
9+
const show = useCallback(() => setVisible(true), []);
10+
11+
return [visible, hide, show] as [boolean, () => void, () => void];
12+
};
13+
14+
export default useModal;

src/pages/about/index.jsx

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from "react";
2+
import { Form, Modal, Input } from "antd";
3+
4+
interface IProps {
5+
visible: boolean;
6+
onCancel: () => void;
7+
onOk: (values: any) => void;
8+
defaultValues: any;
9+
}
10+
11+
const EditModal: React.FunctionComponent<IProps> = (props) => {
12+
const { visible, onCancel, onOk, defaultValues } = props;
13+
const [form] = Form.useForm();
14+
const { validateFields, resetFields } = form;
15+
16+
/**
17+
* 提交
18+
*/
19+
const handleSubmit = () => {
20+
validateFields().then((values) => {
21+
onOk(values);
22+
});
23+
};
24+
25+
/**
26+
* 取消
27+
*/
28+
const handleClose = () => {
29+
resetFields();
30+
onCancel();
31+
};
32+
33+
return (
34+
<Modal visible={visible} title="编辑" onCancel={handleClose} onOk={handleSubmit}>
35+
<Form name="basic" initialValues={defaultValues}>
36+
<Form.Item label="name" name="name">
37+
<Input />
38+
</Form.Item>
39+
40+
<Form.Item label="age" name="age">
41+
<Input />
42+
</Form.Item>
43+
44+
<Form.Item label="address" name="address">
45+
<Input />
46+
</Form.Item>
47+
</Form>
48+
</Modal>
49+
);
50+
};
51+
52+
export default EditModal;

src/pages/dashboard/components/action.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/pages/dashboard/config/columns.tsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/pages/dashboard/index.tsx

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,82 @@
11
import React from "react";
2+
import useModal from "../../hooks/useModal";
23
import { Table, Button } from "antd";
3-
import { columns } from "./config/columns";
44
import { fetchAllData } from "../../services/dashborad";
5+
import EditForm from "./components/EditForm";
56
import "./index.scss";
67

8+
interface IItems {
9+
name: string;
10+
age: number;
11+
address: string;
12+
}
13+
714
const Dashborad = () => {
8-
const [dataSource, changeDataSource] = React.useState();
15+
const [visible, hide, show] = useModal();
16+
17+
const handleEdit = (item: {}) => {
18+
show();
19+
setDefaultItems(item);
20+
};
21+
22+
const columns = [
23+
{
24+
title: "姓名",
25+
dataIndex: "name",
26+
key: "name"
27+
},
28+
{
29+
title: "年龄",
30+
dataIndex: "age",
31+
key: "age"
32+
},
33+
{
34+
title: "住址",
35+
dataIndex: "address",
36+
key: "address"
37+
},
38+
{
39+
title: "操作",
40+
dataIndex: "action",
41+
key: "action",
42+
render: (text: string, record: IItems) => (
43+
<div className="actionWrap">
44+
<span onClick={() => handleEdit(record)}>编辑</span>
45+
<span>删除</span>
46+
</div>
47+
)
48+
}
49+
];
50+
51+
const [dataSource, changeDataSource] = React.useState<IItems[]>([]);
52+
53+
const [defaultItems, setDefaultItems] = React.useState<IItems | {}>({});
954

1055
React.useEffect(() => {
1156
fetchData();
1257
}, []);
1358

14-
// 请求列表数据
59+
/**
60+
* 请求列表数据
61+
*/
1562
const fetchData = async () => {
1663
const res = await fetchAllData();
1764
changeDataSource(res);
1865
};
1966

67+
const update = () => {
68+
hide();
69+
};
70+
2071
return (
2172
<>
2273
<Button type="primary" className="addBtn">
2374
添加
2475
</Button>
2576
<Table dataSource={dataSource} columns={columns} />;
77+
{visible && (
78+
<EditForm visible={visible} onOk={update} onCancel={hide} defaultValues={defaultItems} />
79+
)}
2680
</>
2781
);
2882
};

src/pages/test/A.jsx

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/pages/test/B.jsx

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)