-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter11.html
205 lines (184 loc) · 4.73 KB
/
Chapter11.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Chapter 10 Events!</title>
<script src="https://unpkg.com/react@15.6.1/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.6.1/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);
console.log('Constructor');
}
render() {
console.log('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((prevState, props) => ({count: prevState.count+1}));
}
// or use property initializer syntax to remove the need to call bind for this.increase in constructor()
/*
increase = (e) => {...}
*/
componentWillMount() {
console.log('componentWillMount - but just use constructor');
}
componentDidMount() {
console.log('componentDidMount');
}
/* Updating */
componentWillReceiveProps(nextProps) {
console.log('componentWillReceiveProps', nextProps);
}
shouldComponentUpdate(nextProps, nextState) {
console.log('shouldComponentUpdate?', nextProps, nextState);
if (nextState.count < 5) {
console.log('Yes');
return true;
} else {
ReactDOM.unmountComponentAtNode(destination);
console.log('No');
return false;
}
}
componentWillUpdate(nextProps, nextState) {
console.log('componentWillUpdate', nextProps, nextState);
}
componentDidUpdate(prevProps, prevState) {
console.log('componentDidUpdate', prevProps, prevState);
}
componentWillUnmount() {
console.log('componentWillUnmount');
}
}
class LightningCounter extends React.Component {
constructor(props) {
super(props);
this.state = {
strikes: 0
};
// option 1: bind timerTick to this here
this.timerTick = this.timerTick.bind(this);
}
componentDidMount() {
this.timerId =
//setInterval(this.timerTick, 1000); // option 1, continued
// option 2: bind timerTick here (not preferred, since this has to be done every time timerTick is called)
//setInterval(this.timerTick.bind(this), 1000); //without bind(), the "this" inside timerTick() references the window
// option 3: use arrow function to get the right `this` binding
setInterval(()=> {
//this.setState((prevState, props) => ({strikes: prevState.strikes+100}));
this.timerTick();
}, 1000);
}
componentWillUnmount() {
clearInterval(this.timerId);
}
render() {
const counterStyle = {
color: "#66FFFF",
fontSize: 50
};
const count = this.state.strikes.toLocaleString();
return <h1 style={counterStyle}>{count}</h1>;
}
timerTick() {
console.log('timer');
this.setState((prevState, props) => ({strikes: prevState.strikes+100}));
}
}
class LightningCounterDisplay extends React.Component {
render() {
const commonStyle = {
margin: 0,
padding: 0
};
const divStyle = {
width: 250,
textAlign: "center",
backgroundColor: "#020202",
padding: 40,
fontFamily: "sans-serif",
color: "#999999",
borderRadius: 10
};
const textStyles = {
emphasis: {
fontSize: 38,
...commonStyle
},
smallEmphasis: {
...commonStyle
},
small: {
fontSize: 17,
opacity: 0.5,
...commonStyle
}
}
return <div style={divStyle}>
<LightningCounter/>
<h2 style={textStyles.smallEmphasis}>LIGHTNING STRIKES</h2>
<h2 style={textStyles.emphasis}>WORLDWIDE</h2>
<p style={textStyles.small}>(since you loaded this example)</p>
</div>;
}
}
const destination = document.querySelector("#container");
ReactDOM.render(
<div>
<CounterParent/>
<LightningCounterDisplay/>
</div>, destination);
</script>
</body>
</html>