相当于vue 中 子组件的props, 对传递的属性进行限制

    • 三种应用
      • 类型验证
      • 必选项验证
      • 默认值验证
    1. import React from 'react';
    2. import PropTypes from 'prop-types'
    3. export default class PropsTypesDemo extends React.Component {
    4. constructor() {
    5. super()
    6. this.state = {}
    7. }
    8. render () {
    9. return (
    10. <div>
    11. {this.props.title}
    12. </div>
    13. )
    14. }
    15. }
    16. // 验证传的属性
    17. PropsTypesDemo.propTypes = {
    18. title: PropTypes.string.isRequired // 必选项
    19. }
    20. // 默认值验证
    21. PropsTypesDemo.defaultProps = {
    22. title: '啦啦啦'
    23. }