-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter10.html
88 lines (82 loc) · 2.26 KB
/
Chapter10.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Chapter 10 Events!</title>
<script src="https://unpkg.com/[email protected]/dist/react.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
<style>
#container {
padding: 50px;
background-color: #FFF;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- Your custom script here -->
<script type="text/babel">
class Counter extends React.Component {
render() {
const textStyle = {
fontSize: 72,
fontFamily: "sans-serif",
color: "#333",
fontWeight: "bold"
};
return <div style={textStyle}>
{this.props.display}
</div>;
}
}
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {count: 0};
// this binding is necessary to make `this` work in the button handler
this.increase = this.increase.bind(this);
}
render() {
const backgroundStyle = {
padding: 50,
backgroundColor: "#FFC53A",
width: 250,
height: 100,
borderRadius: 10,
textAlign: "center"
};
const buttonStyle = {
fontSize: "1em",
width: 30,
height: 30,
fontFamily: "sans-serif",
color: "#333",
fontWeight: "bold",
lineHeight: "3px" //px is not automatically added for lineHeight
};
return <div style={backgroundStyle}>
<Counter display={this.state.count}/>
<button onClick={this.increase} style={buttonStyle}>+</button>
</div>;
}
increase(e) {
// this.setState() is async, so need to capture the shiftKey flag here before calling setState()
// since synthetic events are reused due to event pooling, referencing `e` inside setState() will trigger a warning
// due to e being released/nullified, and would need a e.persist() to keep it around. Easier if can avoid that.
const shift = e.shiftKey;
this.setState((prevState, props) => ({count: prevState.count+(shift?10:1)}));
}
// or use property initializer syntax to remove the need to call bind for this.increase in constructor()
/*
increase = (e) => {...}
*/
}
const destination = document.querySelector("#container");
ReactDOM.render(
<div>
<CounterParent/>
</div>, destination);
</script>
</body>
</html>