-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-class-components-with-isolated-state.html
102 lines (89 loc) · 2.26 KB
/
1-class-components-with-isolated-state.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
<!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, Component, Fragment } = React;
const { render } = ReactDOM;
//
// React components
class Counter extends Component {
constructor() {
super();
this.state = { count: 0 };
}
decreaseCount() {
this.setState({ count: this.state.count - 1 });
}
increaseCount() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div>
<button onClick={() => this.increaseCount()}>+</button>
<button onClick={() => this.decreaseCount()}>-</button>
<span>Current count: {this.state.count}</span>
</div>
);
}
}
class Textbox extends Component {
constructor() {
super();
this.state = { text: "" };
}
componentDidMount() {
setTimeout(() => {
this.setState({ text: "Done!" });
}, 1000);
}
setText(value) {
this.setState({ text: value });
}
render() {
return (
<div>
<input
onChange={e => this.setText(e.target.value)}
type="text"
value={this.state.text}
/>
<span>Current text: {this.state.text}</span>
</div>
);
}
}
class App extends Component {
render() {
return (
<Fragment>
<Counter />
<Textbox />
</Fragment>
);
}
}
//
// React bootstrap
render(createElement(App), document.querySelector("#app"));
</script>
</body>
</html>