L7:propTypes应用

介绍

修改的是 src/index.js

JavaScript 是 弱语言。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Comment extends Component {
render(){
const {comment} = this.props;
return(
<div>
<div>
<span>{comment.username}</span>
</div>
<p>{comment.content}</p>
</div>
)
}
}
ReactDOM.render(
<Comment comment={1} />,
document.getElementById('root')
);

传个1 一点错误都不会报。

如果加上PropTypes

1
2
3
4
5
6
import React, { Component, PropTypes } from 'react';
static propTypes = {
comment : PropTypes.object, //必须是object
test:PropTypes.object.isRequired //再强调组件参数必须传入
}

这个时候:

对于 PropTypes 还有其他的各种类型,具体见链接