1.PropTypes
数据类型校验,当父组件传递的数据与子组件规定的数据类型不一样时会报警告错误。
下面的代码是子组件规定的数据类型
组件名.propTypes = {
test: PropTypes.string.isRequired,//isRequired必须传递
content: PropTypes.string,
deleteIntem: PropTypes.func,
index: PropTypes.number
}
2.DefaultProps
默认值,与例1的test属性相结合,test是一个必填的值,当父组件没有传递test值时,则会自动调用defaultProps方法
TodoItem.defaultProps = {
test: 'hello world'
}
详细内容可以查看react官网文档 https://reactjs.org/docs/typechecking-with-proptypes.html