Skip to content

Commit c54060e

Browse files
committed
add lifecycle methods of mounting and updating
1 parent da36c13 commit c54060e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

chapter-02/controlpanel/src/ControlPanel.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ const style = {
66
};
77

88
class ControlPanel extends Component {
9-
109
render() {
10+
console.log('enter ControlPanel render');
1111
return (
1212
<div style={style}>
13-
<Counter caption="First" />
13+
<Counter caption="First"/>
1414
<Counter caption="Second" initValue={10} />
1515
<Counter caption="Third" initValue={20} />
16+
<button onClick={ () => this.forceUpdate() }>
17+
Click me to re-render!
18+
</button>
1619
</div>
1720
);
1821
}

chapter-02/controlpanel/src/Counter.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const buttonStyle = {
77
class Counter extends Component {
88

99
constructor(props) {
10+
console.log('enter constructor: ' + props.caption);
1011
super(props);
1112

1213
this.onClickIncrementButton = this.onClickIncrementButton.bind(this);
@@ -17,6 +18,28 @@ class Counter extends Component {
1718
}
1819
}
1920

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+
2043
onClickIncrementButton() {
2144
this.setState({count: this.state.count + 1});
2245
}
@@ -25,7 +48,13 @@ class Counter extends Component {
2548
this.setState({count: this.state.count - 1});
2649
}
2750

51+
shouldComponentUpdate(nextProps, nextState) {
52+
return (nextProps.caption !== this.props.caption) ||
53+
(nextState.count !== this.state.count);
54+
}
55+
2856
render() {
57+
console.log('enter render ' + this.props.caption);
2958
const {caption} = this.props;
3059
return (
3160
<div>

0 commit comments

Comments
 (0)