File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed
chapter-02/controlpanel/src Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -6,13 +6,16 @@ const style = {
6
6
} ;
7
7
8
8
class ControlPanel extends Component {
9
-
10
9
render ( ) {
10
+ console . log ( 'enter ControlPanel render' ) ;
11
11
return (
12
12
< div style = { style } >
13
- < Counter caption = "First" />
13
+ < Counter caption = "First" />
14
14
< Counter caption = "Second" initValue = { 10 } />
15
15
< Counter caption = "Third" initValue = { 20 } />
16
+ < button onClick = { ( ) => this . forceUpdate ( ) } >
17
+ Click me to re-render!
18
+ </ button >
16
19
</ div >
17
20
) ;
18
21
}
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ const buttonStyle = {
7
7
class Counter extends Component {
8
8
9
9
constructor ( props ) {
10
+ console . log ( 'enter constructor: ' + props . caption ) ;
10
11
super ( props ) ;
11
12
12
13
this . onClickIncrementButton = this . onClickIncrementButton . bind ( this ) ;
@@ -17,6 +18,28 @@ class Counter extends Component {
17
18
}
18
19
}
19
20
21
+ /*
22
+ getInitialState() {
23
+ console.log('enter getInitialState');
24
+ }
25
+
26
+ getDefaultProps() {
27
+ console.log('enter getDefaultProps');
28
+ }
29
+ */
30
+
31
+ componentWillReceiveProps ( nextProps ) {
32
+ console . log ( 'enter componentWillReceiveProps ' + this . props . caption )
33
+ }
34
+
35
+ componentWillMount ( ) {
36
+ console . log ( 'enter componentWillMount ' + this . props . caption ) ;
37
+ }
38
+
39
+ componentDidMount ( ) {
40
+ console . log ( 'enter componentDidMount ' + this . props . caption ) ;
41
+ }
42
+
20
43
onClickIncrementButton ( ) {
21
44
this . setState ( { count : this . state . count + 1 } ) ;
22
45
}
@@ -25,7 +48,13 @@ class Counter extends Component {
25
48
this . setState ( { count : this . state . count - 1 } ) ;
26
49
}
27
50
51
+ shouldComponentUpdate ( nextProps , nextState ) {
52
+ return ( nextProps . caption !== this . props . caption ) ||
53
+ ( nextState . count !== this . state . count ) ;
54
+ }
55
+
28
56
render ( ) {
57
+ console . log ( 'enter render ' + this . props . caption ) ;
29
58
const { caption} = this . props ;
30
59
return (
31
60
< div >
You can’t perform that action at this time.
0 commit comments