正常写时
export default function Content({ part, exercises }) { | |
return ( | |
<p>{part} {exercises} </p> | |
); | |
} |
会有报错: ...is missing in props validation
解决方法:
- 添加
propTypes
- 为组件添加
propTypes
类型校验
import PropTypes from 'prop-types'; | |
export default function Content({ part, exercises }) { | |
return ( | |
<p>{part} {exercises} </p> | |
); | |
} | |
Content.propTypes = { | |
part: PropTypes.string.isRequired, // 作用是验证 props | |
exercises: PropTypes.number.isRequired // 作用是验证 props | |
} |
如果未安装 prop-types
,则需要安装: npm install prop-types