Skip to content

Commit 1bf579e

Browse files
author
Yauhen
committed
Lesson 04: Props (Part I)
1 parent 6a70562 commit 1bf579e

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"prop-types": "^15.6.2",
67
"react": "^16.4.2",
78
"react-dom": "^16.4.2",
89
"react-scripts": "1.1.5"
@@ -13,4 +14,4 @@
1314
"test": "react-scripts test --env=jsdom",
1415
"eject": "react-scripts eject"
1516
}
16-
}
17+
}

src/04_props/Lesson.jsx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
const Counter = ({ counter, func, number, string }) => {
5+
return <h1>{`Counter component. Counter value is: ${counter}`}</h1>
6+
}
7+
8+
Counter.propTypes = {
9+
counter: PropTypes.number.isRequired,
10+
func: PropTypes.func,
11+
number: PropTypes.number,
12+
string: PropTypes.string,
13+
}
14+
15+
Counter.defaultProps = {
16+
func: () => {},
17+
number: 0,
18+
string: '',
19+
}
20+
21+
class CounterButton extends Component {
22+
state = {
23+
counter: 0,
24+
}
25+
26+
handleClick = () => {
27+
this.setState(({ counter }) => ({
28+
counter: ++counter,
29+
}))
30+
}
31+
32+
render() {
33+
const { counter } = this.state;
34+
35+
return (
36+
<div>
37+
<div>{counter}</div>
38+
<Counter
39+
counter={counter}
40+
/>
41+
<button onClick={this.handleClick}>+1</button>
42+
</div>
43+
);
44+
}
45+
}
46+
47+
export default CounterButton;
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
//

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import CounterButton from './03_state/Lesson.jsx';
3+
import Lesson from './04_props/Lesson.jsx';
44
import registerServiceWorker from './registerServiceWorker';
55

6-
ReactDOM.render(<CounterButton />, document.getElementById('root'));
6+
ReactDOM.render(<Lesson />, document.getElementById('root'));
77
registerServiceWorker();

0 commit comments

Comments
 (0)