相当于vue 中 子组件的props, 对传递的属性进行限制
- 三种应用
- 类型验证
- 必选项验证
- 默认值验证
import React from 'react';
import PropTypes from 'prop-types'
export default class PropsTypesDemo extends React.Component {
constructor() {
super()
this.state = {}
}
render () {
return (
<div>
{this.props.title}
</div>
)
}
}
// 验证传的属性
PropsTypesDemo.propTypes = {
title: PropTypes.string.isRequired // 必选项
}
// 默认值验证
PropsTypesDemo.defaultProps = {
title: '啦啦啦'
}