函数式组件,props 默认值与类型检查
- defaultProps -> props 默认值
function Main(props) {
return (
<div>{props.name}</div>
)
}
Main.defaultProps = {
name: '5k'
}
- propTypes -> 对 props 进行类型检查
import PropTypes from 'prop-types';
function Main(props) {
return (
<div>{props.name}</div>
)
}
Main.PropTyps = {
name: PropTypes.String
}
function App() {
return (
{
}
<Main name={'5k'} />
)
}
类组件,props 默认值与类型检查
import PropTypes from 'prop-types';
import React from 'react'
class Main extends React.Component {
constructor(props) {
super(props);
}
render(){
return (
<div>{this.props.name}</div>
)
}
static defultProps = {
name: '5k'
}
static propTypes = {
name: PropTypes.string
}
}
export default Main