Skip to content

Commit f42eb37

Browse files
committed
use defaultProps
1 parent 0a0ff8d commit f42eb37

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

chapter-02/demo/src/ControlPanel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ControlPanel extends Component {
1010
render() {
1111
return (
1212
<div style={style}>
13-
<Counter caption="First" initValue={0} />
13+
<Counter caption="First" foo/>
1414
<Counter caption="Second" initValue={10} />
1515
<Counter caption="Third" initValue={20} />
1616
</div>

chapter-02/demo/src/Counter.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Counter extends Component {
1313
this.onClickDecrementButton = this.onClickDecrementButton.bind(this);
1414

1515
this.state = {
16-
count: props.initValue || 0
16+
count: props.initValue
1717
}
1818
}
1919

@@ -37,5 +37,14 @@ class Counter extends Component {
3737
}
3838
}
3939

40+
Counter.propTypes = {
41+
caption: PropTypes.string.isRequired,
42+
initValue: PropTypes.number
43+
};
44+
45+
Counter.defaultProps = {
46+
initValue: 0
47+
};
48+
4049
export default Counter;
4150

0 commit comments

Comments
 (0)