美文网首页
PropTypes和defaultProps

PropTypes和defaultProps

作者: 参商_70a0 | 来源:发表于2019-10-08 09:56 被阅读0次

react中可以引入PropTypes来对props进行类型的校验
import PropTypes from "prop-types";
TodoItem.propTypes={
itemContent:PropTypes.string.isRequired,
ItemDelete:PropTypes.func,
index:PropTypes.oneOfType( [ PropTypes.number,PropTypes.string ] )
}
isRequired代表必须存在。

TodoItem.defaultProps={
test:'hello world'
}
如果没有定义test,则test的默认值为“hello world”

相关文章

网友评论

      本文标题:PropTypes和defaultProps

      本文链接:https://www.haomeiwen.com/subject/jzohuctx.html