-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-propTypes.html
68 lines (55 loc) · 1.69 KB
/
04-propTypes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<div id="root"></div>
<script src="http://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="http://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script src="https://unpkg.com/[email protected]/prop-types.js"></script>
<script type="text/babel">
const rootElement = document.getElementById('root')
// function SayHello (props) {
// return (
// <div>
// Hello {props.firstName} {props.lastName}!
// </div>
// )
// }
class SayHello extends React.Component {
static propTypes = {
firstName: PropTypes.string.isRequired,
lastName: PropTypes.string.isRequired
}
render() {
const {firstName, lastName} = this.props
return (
<div>
Hello {firstName} {lastName}!
</div>
)
}
}
// 1.
// SayHello.propTypes = {
// firstName(props, propName, componentName) {
// if(typeof props[propName] !== 'string') {
// return new Error(`Hey, you should pass a string for ${propName} in ${componentName} but you passed the type ${typeof props[propName]}`)
// }
// }
// }
// 2.
// const PropTypes = {
// string (props, propName, componentName) {
// if(typeof props[propName] !== 'string') {
// return new Error(`Hey, you should pass a string for ${propName} in ${componentName} but you passed the type ${typeof props[propName]}`)
// }
// }
// }
// 3.
// SayHello.propTypes = {
// firstName: PropTypes.string,
// lastName: PropTypes.string
// }
SayHello.propTypes = {
firstName: PropTypes.string.isRequired,
lastName: PropTypes.string.isRequired
}
ReactDOM.render(<SayHello firstName={true} />, rootElement)
</script>