Skip to content

Commit

Permalink
add lifecycle methods of mounting and updating
Browse files Browse the repository at this point in the history
  • Loading branch information
mocheng committed Dec 17, 2016
1 parent da36c13 commit c54060e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions chapter-02/controlpanel/src/ControlPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ const style = {
};

class ControlPanel extends Component {

render() {
console.log('enter ControlPanel render');
return (
<div style={style}>
<Counter caption="First" />
<Counter caption="First"/>
<Counter caption="Second" initValue={10} />
<Counter caption="Third" initValue={20} />
<button onClick={ () => this.forceUpdate() }>
Click me to re-render!
</button>
</div>
);
}
Expand Down
29 changes: 29 additions & 0 deletions chapter-02/controlpanel/src/Counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const buttonStyle = {
class Counter extends Component {

constructor(props) {
console.log('enter constructor: ' + props.caption);
super(props);

this.onClickIncrementButton = this.onClickIncrementButton.bind(this);
Expand All @@ -17,6 +18,28 @@ class Counter extends Component {
}
}

/*
getInitialState() {
console.log('enter getInitialState');
}
getDefaultProps() {
console.log('enter getDefaultProps');
}
*/

componentWillReceiveProps(nextProps) {
console.log('enter componentWillReceiveProps ' + this.props.caption)
}

componentWillMount() {
console.log('enter componentWillMount ' + this.props.caption);
}

componentDidMount() {
console.log('enter componentDidMount ' + this.props.caption);
}

onClickIncrementButton() {
this.setState({count: this.state.count + 1});
}
Expand All @@ -25,7 +48,13 @@ class Counter extends Component {
this.setState({count: this.state.count - 1});
}

shouldComponentUpdate(nextProps, nextState) {
return (nextProps.caption !== this.props.caption) ||
(nextState.count !== this.state.count);
}

render() {
console.log('enter render ' + this.props.caption);
const {caption} = this.props;
return (
<div>
Expand Down

0 comments on commit c54060e

Please sign in to comment.