react子组件类型验证器合集

React . createClass ( { propTypes : { // 可以声明 prop 为指定的 JS 基本数据类型,默认情况,这些数据是可选的 optionalArray : React . PropTypes . array , optionalBool : React . PropTypes . bool , optionalFunc : React . PropTypes . func , optionalNumber : React . PropTypes . number , optionalObject : React . PropTypes . object , optionalString : React . PropTypes . string , // 可以被渲染的对象 numbers, strings, elements 或 array optionalNode : React . PropTypes . node , // React 元素 optionalElement : React . PropTypes . element , // 用 JS 的 instanceof 操作符声明 prop 为类的实例。 optionalMessage : React . PropTypes . instanceOf ( Message ) , // 用 enum 来限制 prop 只接受指定的值。 optionalEnum : React . PropTypes . oneOf ( [ ' News ' , ' Photos ' ] ) , // 可以是多个对象类型中的一个 optionalUnion : React . PropTypes . oneOfType ( [ React . PropTypes . string , React . PropTypes . number , React . PropTypes . instanceOf ( Message ) ] ) , // 指定类型组成的数组 optionalArrayOf : React . PropTypes . arrayOf ( React . PropTypes . number ) , // 指定类型的属性构成的对象 optionalObjectOf : React . PropTypes . objectOf ( React . PropTypes . number ) , // 特定 shape 参数的对象 optionalObjectWithShape : React . PropTypes . shape ( { color : React . PropTypes . string , fontSize : React . PropTypes . number } ) , // 任意类型加上 `isRequired` 来使 prop 不可空。 requiredFunc : React . PropTypes . func . isRequired , // 不可空的任意类型 requiredAny : React . PropTypes . any . isRequired , // 自定义验证器。如果验证失败需要返回一个 Error 对象。不要直接使用 `console.warn` 或抛异常,因为这样 `oneOfType` 会失效。 customProp : function ( props , propName , componentName ) { if ( !/ matchme /. test ( props [ propName ] ) ) { return new Error ( ' Validation failed! ' ) ; } } } , /* ... */ } ) ;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值