React PropTypes
2015-10-20 00:00
387 查看
PropTypes用于指定component参数类型或是否required等,起到校验作用,使用PropTypes有两个好处:
1. You can easily open up a component and check which props are required and what type they should be.
2. When things get messed up React will give you an awesome error message in the console, saying which props is wrong/missing plus the render method that caused the problem.
PropTypes中的参数类型有:
如果参数传入不正确,则console会出现类似以下warning:
如果指定了propTypes,但使用Component时不想传入任何参数,可以使用getDefaultProps();
1. You can easily open up a component and check which props are required and what type they should be.
2. When things get messed up React will give you an awesome error message in the console, saying which props is wrong/missing plus the render method that caused the problem.
PropTypes中的参数类型有:
React.createClass({ propTypes: { optionalArray: React.PropTypes.array, optionalBool: React.PropTypes.bool, optionalFunc: React.PropTypes.func, optionalNumber: React.PropTypes.number, optionalObject: React.PropTypes.object, optionalString: React.PropTypes.string, // 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, //参数为function,且必须 // A value of any data type requiredAny: React.PropTypes.any.isRequired, //自定义参数校验类型 customProp: function(props, propName, componentName) { if (!/matchme/.test(props[propName])) { return new Error('Validation failed!'); } } }, /* ... */ });
如果参数传入不正确,则console会出现类似以下warning:
如果指定了propTypes,但使用Component时不想传入任何参数,可以使用getDefaultProps();
var ComponentWithDefaultProps = React.createClass({ getDefaultProps: function() { return { value: 'default value' }; } /* ... */ });
相关文章推荐
- React Mixin
- React-Native for Android
- React-Native进行时(四)--ListView navigator.push
- ACE_Reactor(六)ACE_TP_Reactor
- ACE_Reactor(五)ACE_TP_Reactor和ACE_Select_Reactor的区别
- ACE_Reactor(四):ACE_WFMO_Reactor
- ACE_Reactor(三)ACE_Select_Reactor_T
- ACE_Reactor(二)ACE_Dev_Poll_Reactor
- ACE_Reactor(一)整体理解ACE_Reactor
- React.js model
- React Native For Android 架构初探
- An iOS Developer on React Native一个资深iOS开发者对于React Native具体使用体验
- React Native for Android 实践 — 实现知乎日报客户端
- React Native For Android 架构初探
- React环境搭建
- react native andrroid 学习相关的资料
- React Native 文件模板配置
- ReactiveCocoa基础篇
- Spring reactor框架简介
- ReactiveCocoa 谈谈RACMulticastConnection