react属性校验
2024-08-29 02:44:09
https://reactjs.org/docs/typechecking-with-proptypes.html
1.安装:cnpm i prop-types -S
import PropTypes from 'prop-types';
MyComponent.propTypes = {
optionalArray: PropTypes.array,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
optionalNumber: PropTypes.number,
optionalObject: PropTypes.object,
optionalString: PropTypes.string,
optionalSymbol: PropTypes.symbol,
// Anything that can be rendered: numbers, strings, elements or an array
// (or fragment) containing these types.
optionalNode: PropTypes.node,
// A React element.
// 元素对象
optionalElement: PropTypes.element,
// You can also declare that a prop is an instance of a class. This uses
// JS's instanceof operator.
// 判断是否是某个类型
optionalMessage: PropTypes.instanceOf(Person),
// You can ensure that your prop is limited to specific values by treating
// it as an enum.
// 两个类型满足一个
optionalEnum: PropTypes.oneOf(['Person', 'Man']),
// An object that could be one of many types
optionalUnion: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Message)
]),
// An array of a certain type
// 数组中的值全为数字
optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
// An object with property values of a certain type
// 对象中的value值全为数字
optionalObjectOf: PropTypes.objectOf(PropTypes.number),
// An object taking on a particular shape
optionalObjectWithShape: PropTypes.shape({
color: PropTypes.string,
fontSize: PropTypes.number
}),
// You can chain any of the above with `isRequired` to make sure a warning
// is shown if the prop isn't provided.
requiredFunc: PropTypes.func.isRequired,
// A value of any data type
requiredAny: PropTypes.any.isRequired,
// You can also specify a custom validator. It should return an Error
// object if the validation fails. Don't `console.warn` or throw, as this
// won't work inside `oneOfType`.
customProp: function(props, propName, componentName) {
if (!/matchme/.test(props[propName])) {
return new Error(
'Invalid prop `' + propName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
},
// You can also supply a custom validator to `arrayOf` and `objectOf`.
// It should return an Error object if the validation fails. The validator
// will be called for each key in the array or object. The first two
// arguments of the validator are the array or object itself, and the
// current item's key.
customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
})
};
..
最新文章
- OrchardNoCMS vNext如何在VS2015下调试
- LeetCode() Range Sum Query-mutable
- ACM: HDU 2544 最短路-Dijkstra算法
- 在为知笔记中使用JQuery
- hadoop fs管理文件权限
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-002使用@Embeddable
- Linux下修改Oracle监听地址
- Java并发编程:sleep、wait、yield对比
- Managing TCP Connections in Dynamic Spectrum Access Based Wireless LANs
- Struts2中获取HttpServletRequest,HttpSession等的几种方式
- Linux shell用法和技巧(转)
- cocos2dx - 控件扩展之pageview循环显示
- javascript中的null,对象系统还是非对象系统?
- 数据包接收系列 — NAPI的原理和实现
- WebForm+一般处理程序+Ajax聊天
- spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值
- AspectJ使用的遇到的坑
- 字符常量 java
- 学习笔记之Sublime Text
- mitmproxy
热门文章
- jQuery Plugin Poshy Tip 使用 统一提示信息
- Chapter11
- CodeForces - 589B
- python3+Appium自动化06-屏幕截图
- java里如何实现对数组中的元素反转[4, 1, 8, 7, 3, 8, 2]变成 [2, 8, 3, 7, 8, 1, 4]
- bash和zsh切换
- JDK11 | 第七篇 : ZGC 垃圾收集器
- electron 开发记录
- win10命令行压缩zip文件
- Centos 6.5 修改默认分辨率