博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
React组件属性类型(propTypes)
阅读量:5068 次
发布时间:2019-06-12

本文共 2998 字,大约阅读时间需要 9 分钟。

React.createClass({  propTypes: {    // You can declare that a prop is a specific JS primitive. By default, these    // are all optional.    optionalArray: React.PropTypes.array,    optionalBool: React.PropTypes.bool,    optionalFunc: React.PropTypes.func,    optionalNumber: React.PropTypes.number,    optionalObject: React.PropTypes.object,    optionalString: React.PropTypes.string,    optionalSymbol: React.PropTypes.symbol,    // Anything that can be rendered: numbers, strings, elements or an array    // (or fragment) containing these types.    optionalNode: React.PropTypes.node,    // A React element.    optionalElement: React.PropTypes.element,    // You can also declare that a prop is an instance of a class. This uses    // JS's instanceof operator.    optionalMessage: React.PropTypes.instanceOf(Message),    // You can ensure that your prop is limited to specific values by treating    // it as an enum.    optionalEnum: React.PropTypes.oneOf(['News', 'Photos']),    // An object that could be one of many types    optionalUnion: React.PropTypes.oneOfType([      React.PropTypes.string,      React.PropTypes.number,      React.PropTypes.instanceOf(Message)    ]),    // An array of a certain type    optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),    // An object with property values of a certain type    optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),    // An object taking on a particular shape    optionalObjectWithShape: React.PropTypes.shape({      color: React.PropTypes.string,      fontSize: React.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: React.PropTypes.func.isRequired,    // A value of any data type    requiredAny: React.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: React.PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {      if (!/matchme/.test(propValue[key])) {        return new Error(          'Invalid prop `' + propFullName + '` supplied to' +          ' `' + componentName + '`. Validation failed.'        );      }    })  },  /* ... */});

 

转载于:https://www.cnblogs.com/yiguozhi/p/6612779.html

你可能感兴趣的文章
Effective Java 74 Implement Serializable judiciously
查看>>
Java Concurrency In Practice -Chapter 2 Thread Safety
查看>>
13.constexpr
查看>>
15.map映射
查看>>
flask简单应用以及配置文件的写法。
查看>>
n阶螺旋矩阵问题
查看>>
工作中碰到的一些问题以及解决方法
查看>>
C#-WinForm-对话框控件
查看>>
支持多个版本的ASP.NET Core Web API
查看>>
D - Xenia and Bit Operations
查看>>
tar用法
查看>>
Spring框架初识(二)
查看>>
redis设置密码
查看>>
【数据仓库】数据仓库概述
查看>>
form表单传对象
查看>>
中科燕园GIS外包--环境保护执法移动GIS平台
查看>>
Fibonacci博弈
查看>>
JSP中的九个内置对象(17)
查看>>
ContentProvider和Uri详解
查看>>
(转)ios学习--你会遇到的runtime面试题(详)
查看>>