-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-function-components-with-isolated-state-using-hooks.html
79 lines (69 loc) · 1.79 KB
/
4-function-components-with-isolated-state-using-hooks.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>React playground</title>
</head>
<body>
<div id="app"></div>
<!-- react -->
<script
crossorigin
src="https://unpkg.com/react@16/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
></script>
<!-- babel -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
//
// Imports
const { createElement, Fragment, useEffect, useState } = React;
const { render } = ReactDOM;
//
// React components
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>+</button>
<button onClick={() => setCount(count - 1)}>-</button>
<span>Current count: {count}</span>
</div>
);
}
function Textbox() {
const [text, setText] = useState("");
useEffect(() => {
// componentDidMount (no componentDidUpdate because of the `[]` argument)
setTimeout(() => {
setText("Done!");
}, 1000);
}, []);
return (
<div>
<input
onChange={e => setText(e.target.value)}
type="text"
value={text}
/>
<span>Current text: {text}</span>
</div>
);
}
function App() {
return (
<Fragment>
<Counter />
<Textbox />
</Fragment>
);
}
//
// React bootstrap
render(createElement(App), document.querySelector("#app"));
</script>
</body>
</html>