Skip to content

Commit 0a0ff8d

Browse files
committed
init ControlPanel and Counter
1 parent d20f726 commit 0a0ff8d

File tree

8 files changed

+65
-96
lines changed

8 files changed

+65
-96
lines changed

chapter-02/demo/src/App.css

Lines changed: 0 additions & 24 deletions
This file was deleted.

chapter-02/demo/src/App.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

chapter-02/demo/src/App.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

chapter-02/demo/src/ClickCounter.js

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { Component } from 'react';
2+
import Counter from './Counter.js';
3+
4+
const style = {
5+
margin: '20px'
6+
};
7+
8+
class ControlPanel extends Component {
9+
10+
render() {
11+
return (
12+
<div style={style}>
13+
<Counter caption="First" initValue={0} />
14+
<Counter caption="Second" initValue={10} />
15+
<Counter caption="Third" initValue={20} />
16+
</div>
17+
);
18+
}
19+
}
20+
21+
export default ControlPanel;
22+

chapter-02/demo/src/Counter.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, { Component, PropTypes } from 'react';
2+
3+
const buttonStyle = {
4+
margin: '10px'
5+
};
6+
7+
class Counter extends Component {
8+
9+
constructor(props) {
10+
super(props);
11+
12+
this.onClickIncrementButton = this.onClickIncrementButton.bind(this);
13+
this.onClickDecrementButton = this.onClickDecrementButton.bind(this);
14+
15+
this.state = {
16+
count: props.initValue || 0
17+
}
18+
}
19+
20+
onClickIncrementButton() {
21+
this.setState({count: this.state.count + 1});
22+
}
23+
24+
onClickDecrementButton() {
25+
this.setState({count: this.state.count - 1});
26+
}
27+
28+
render() {
29+
const {caption} = this.props;
30+
return (
31+
<div>
32+
<button style={buttonStyle} onClick={this.onClickIncrementButton}>+</button>
33+
<button style={buttonStyle} onClick={this.onClickDecrementButton}>-</button>
34+
<span>{caption} count: {this.state.count}</span>
35+
</div>
36+
);
37+
}
38+
}
39+
40+
export default Counter;
41+

chapter-02/demo/src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
//import App from './App';
4-
import ClickCounter from './ClickCounter';
3+
import ControlPanel from './ControlPanel';
54
import './index.css';
65

76
ReactDOM.render(
8-
<ClickCounter/>,
7+
<ControlPanel/>,
98
document.getElementById('root')
109
);

chapter-02/demo/src/logo.svg

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)