Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. React.createClass({
  2. propTypes: {
  3. // 可以声明 prop 为指定的 JS 基本数据类型,默认情况,这些数据是可选的
  4. optionalArray: React.PropTypes.array,
  5. optionalBool: React.PropTypes.bool,
  6. optionalFunc: React.PropTypes.func,
  7. optionalNumber: React.PropTypes.number,
  8. optionalObject: React.PropTypes.object,
  9. optionalString: React.PropTypes.string,
  10.  
  11. // 可以被渲染的对象 numbers, strings, elements 或 array
  12. optionalNode: React.PropTypes.node,
  13.  
  14. // React 元素
  15. optionalElement: React.PropTypes.element,
  16.  
  17. // 用 JS 的 instanceof 操作符声明 prop 为类的实例。
  18. optionalMessage: React.PropTypes.instanceOf(Message),
  19.  
  20. // 用 enum 来限制 prop 只接受指定的值。
  21. optionalEnum: React.PropTypes.oneOf(['News', 'Photos']),
  22.  
  23. // 可以是多个对象类型中的一个
  24. optionalUnion: React.PropTypes.oneOfType([
  25. React.PropTypes.string,
  26. React.PropTypes.number,
  27. React.PropTypes.instanceOf(Message)
  28. ]),
  29.  
  30. // 指定类型组成的数组
  31. optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
  32.  
  33. // 指定类型的属性构成的对象
  34. optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
  35.  
  36. // 特定 shape 参数的对象
  37. optionalObjectWithShape: React.PropTypes.shape({
  38. color: React.PropTypes.string,
  39. fontSize: React.PropTypes.number
  40. }),
  41.  
  42. // 任意类型加上 `isRequired` 来使 prop 不可空。
  43. requiredFunc: React.PropTypes.func.isRequired,
  44.  
  45. // 不可空的任意类型
  46. requiredAny: React.PropTypes.any.isRequired,
  47.  
  48. // 自定义验证器。如果验证失败需要返回一个 Error 对象。不要直接使用 `console.warn` 或抛异常,因为这样 `oneOfType` 会失效。
  49. customProp: function(props, propName, componentName) {
  50. if (!/matchme/.test(props[propName])) {
  51. return new Error('Validation failed!');
  52. }
  53. }
  54. },
  55. /* ... */
  56. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement