安装
npm i prop-types
类式组件
import {
Component} from 'react'
import PropTypes from "prop-types";
class Two extends Component {
static propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number
}
static defaultProps = {
age: 30
}
constructor(props){
super(props)
console.log('123', props)
}
render () {
return (
<div>
<h1>这是Two!!!</h1>
</div>
)
}
}
export default Two
函数式组件
import PropTypes from "prop-types";
function Three (props) {
console.log(props)
return (
<div>
<h1>这是Three了!</h1>
</div>
)
}
Three.propTypes = {
name: PropTypes.string.isRequired
}
Three.defaultProps = {
name: '大锤'
}
export default Three