-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-function-components-with-parent-state.html
115 lines (99 loc) · 2.69 KB
/
2-function-components-with-parent-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
103
104
105
106
107
108
109
110
111
112
113
114
115
<!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>
<!-- redux -->
<script crossorigin src="https://unpkg.com/redux/dist/redux.js"></script>
<!-- react-redux -->
<script
crossorigin
src="https://unpkg.com/react-redux@7.1.0/dist/react-redux.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
function Counter(props) {
const { count, increaseCount, decreaseCount } = props;
return (
<div>
<button onClick={() => increaseCount()}>+</button>
<button onClick={() => decreaseCount()}>-</button>
<span>Current count: {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 {
constructor() {
super();
this.state = { count: 0 };
}
increaseCount = () => {
this.setState({ count: this.state.count + 1 });
};
decreaseCount = () => {
this.setState({ count: this.state.count - 1 });
};
render() {
return (
<Fragment>
<Counter
count={this.state.count}
increaseCount={this.increaseCount}
decreaseCount={this.decreaseCount}
/>
{this.state.count !== 1 ? <Textbox /> : null}
</Fragment>
);
}
}
//
// React bootstrap
render(createElement(App), document.querySelector("#app"));
</script>
</body>
</html>