-
Notifications
You must be signed in to change notification settings - Fork 23
/
React全家桶例子.html
146 lines (127 loc) · 4.14 KB
/
React全家桶例子.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>html react</title>
<!-- jsx -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- react -->
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<!-- router -->
<script src="https://unpkg.com/[email protected]/umd/react-router-dom.min.js"></script>
<!-- antd -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/antd.min.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/antd.min.js"></script>
<!-- Store -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/index.development.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/utils.development.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const { Fragment, useState, useEffect, useRef } = React;
const { Route, Link, HashRouter } = ReactRouterDOM;
const { Button, Drawer } = antd;
const { atom, useAtom } = jotai;
const { atomWithStorage } = jotaiUtils;
// 保存到 localStorage , 页面刷新,数据不丢失
let counterAtom = atomWithStorage("counter", 0);
let MyComponent = (props) => {
const [store] = useAtom(counterAtom);
return (
<div>
<h3>MyComponent</h3>
<p>{props.name}</p>
<p>store: {store}</p>
<p>{props.head("112345", "eqweq")}</p>
<p>{props.children}</p>
<Button type="primary" onClick={props.handleChildClick}>
childClick
</Button>
</div>
);
};
let Home = () => {
const [date, setDate] = useState("--");
const [store, setStore] = useAtom(counterAtom);
return (
<div>
<p>Home</p>
<p>{date}</p>
<p>store: {store}</p>
<Button
type="primary"
onClick={() => {
setStore((p) => p + 1);
}}
>
changeStore
</Button>
<MyComponent
name="我是 props name"
head={(num, str) => {
return (
<span>
我是 head 具名插槽,参数: {num} , {str}
</span>
);
}}
handleChildClick={() => {
setDate(new Date().toLocaleString());
}}
>
我是默认插槽
</MyComponent>
</div>
);
};
let Login = () => {
const [open, setOpen] = useState(false);
const [store, setStore] = useAtom(counterAtom);
let handleDrawer = () => {
setOpen((p) => !p);
};
return (
<div>
<p>store: {store}</p>
<Button type="danger" onClick={handleDrawer}>
open
</Button>
<Drawer title="Basic Drawer" placement="right" onClose={handleDrawer} open={open}>
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</Drawer>
</div>
);
};
let App = () => {
return (
<HashRouter>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/login">Login</Link>
</li>
</ul>
<Route path="/" exact component={Home} />
<Route path="/login" component={Login} />
</HashRouter>
);
};
// 挂载到 id="root"
ReactDOM.createRoot(document.getElementById("root")).render(
<Fragment>
<React.StrictMode>
<App />
</React.StrictMode>
</Fragment>
);
</script>
</body>
</html>