React: Using PropTypes

    技术2026-04-18  2

    下载prop-types

    npm install --save prop-types

    设置proptype

    import React, { Component } from 'react'; // import './Person.css'; import classes from './person.module.css'; import withClass from '../../../hoc/WithClass'; import Aux from '../../../hoc/Auxiliary'; import PropTypes, { number } from 'prop-types'; class Person extends Component{ render(){ console.log('person redering') return( // <div className="Person" style={style}> // <div className={classes.Person}> <React.Fragment> <p>I Am a person!</p> <p onClick = {this.props.click}>{this.props.name}</p> <p>i'm {this.props.age}</p> <p>{this.props.children}</p> <input type="text" onChange={this.props.changed} value={this.props.name}/> </React.Fragment> // </div> ) } }; Person.propTypes ={ click: PropTypes.func, age:PropTypes.number, name:PropTypes.string, changed:PropTypes.func }; export default withClass(Person,classes.Person);

    注意点 Person.propTypes={

    }; 为改component中props参数的默认类型;

    Processed: 0.018, SQL: 12