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.'
);
}
})
};

..

最新文章

  1. OrchardNoCMS vNext如何在VS2015下调试
  2. LeetCode() Range Sum Query-mutable
  3. ACM: HDU 2544 最短路-Dijkstra算法
  4. 在为知笔记中使用JQuery
  5. hadoop fs管理文件权限
  6. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-002使用@Embeddable
  7. Linux下修改Oracle监听地址
  8. Java并发编程:sleep、wait、yield对比
  9. Managing TCP Connections in Dynamic Spectrum Access Based Wireless LANs
  10. Struts2中获取HttpServletRequest,HttpSession等的几种方式
  11. Linux shell用法和技巧(转)
  12. cocos2dx - 控件扩展之pageview循环显示
  13. javascript中的null,对象系统还是非对象系统?
  14. 数据包接收系列 — NAPI的原理和实现
  15. WebForm+一般处理程序+Ajax聊天
  16. spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值
  17. AspectJ使用的遇到的坑
  18. 字符常量 java
  19. 学习笔记之Sublime Text
  20. mitmproxy

热门文章

  1. jQuery Plugin Poshy Tip 使用 统一提示信息
  2. Chapter11
  3. CodeForces - 589B
  4. python3+Appium自动化06-屏幕截图
  5. java里如何实现对数组中的元素反转[4, 1, 8, 7, 3, 8, 2]变成 [2, 8, 3, 7, 8, 1, 4]
  6. bash和zsh切换
  7. JDK11 | 第七篇 : ZGC 垃圾收集器
  8. electron 开发记录
  9. win10命令行压缩zip文件
  10. Centos 6.5 修改默认分辨率