-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React 中的 prop-types #18
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prop-types
propTypes 主要用于对组件之间传递数据的校验,以防止传输的数据类型出错的情况
要使用的话需要下载 prop-types 这个包并引入
一个简单的例子
常见的用法
几个比较特殊的方法
1.
PropTypes.oneOfType
这个方法能检测目标值是否在一个合法的范围中
2..
PropTypes.oneOf
规定目标必须是后面数组的值之一
3.
PropTypes.arrayOf
和PropTypes.objectOf
用于检测数组内或者对象内的值
4..
PropTypes.shape
可以规定对象内多个值的属性,可以理解为3的升级版
5
.isRequired
对于有些传入值,我们不仅要求他符合某种类型,还认为他是必需的,这个时候可以使用
isRequired
PS:如果不要求值的具体类型,但他又是必须时,可以使用
PropTypes.any.isRequired
6.除了上述情况时,面对更复杂的校验,我们可以把PropTypes写成函数进行校验
参考文档
react prop-types的使用讲解
The text was updated successfully, but these errors were encountered: