react类型检查PropTypes
PropTypes 是一个在 编码 阶段提供类型检查的方案
1. PropTypes 使用
npm地址:https://www.npmjs.com/package/prop-types
安装:
1
| npm install --save prop-types
|
导入包:
1 2
| import PropTypes from 'prop-types'; var PropTypes = require('prop-types');
|
使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import React from 'react'; import PropTypes from 'prop-types';
class MyComponent extends React.Component { render() { } }
MyComponent.propTypes = { name: PropTypes.string, history: PropTypes.object.isRequired, }
MyComponent..defaultProps = { name: 'jack' }
|
纯函数的用法:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import PropTypes from 'prop-types';
const FundManagement = props => { const {children} = props; return ( <div className="statistics"> {children} </div> ); }; FundManagement.propTypes = { children: PropTypes.element.isRequired, };
|
2. 不同类型的验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| MyComponent.propTypes = { optionalArray: PropTypes.array, optionalBool: PropTypes.bool, optionalFunc: PropTypes.func, optionalNumber: PropTypes.number, optionalObject: PropTypes.object, optionalString: PropTypes.string, optionalSymbol: PropTypes.symbol,
optionalNode: PropTypes.node,
optionalElement: PropTypes.element, optionalElementType: PropTypes.elementType,
optionalMessage: PropTypes.instanceOf(Message),
optionalEnum: PropTypes.oneOf(['News', 'Photos']),
optionalUnion: PropTypes.oneOfType([ PropTypes.string, PropTypes.number, PropTypes.instanceOf(Message) ]),
optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
optionalObjectOf: PropTypes.objectOf(PropTypes.number),
optionalObjectWithShape: PropTypes.shape({ color: PropTypes.string, fontSize: PropTypes.number }),
requiredFunc: PropTypes.func.isRequired,
requiredAny: PropTypes.any.isRequired,
customProp: function(props, propName, componentName) { if (!/matchme/.test(props[propName])) { return new Error( 'Invalid prop `' + propName + '` supplied to' + ' `' + componentName + '`. Validation failed.' ); } },
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.' ); } }) };
|
3. 为什么要类型检查
下面内容转载自:https://segmentfault.com/a/1190000015071373
我们先来看一份 rollbar 公司对 1000+ 项目的错误回收分析 top10

大部分都是类型错误。
比如定义 let prodList = [] , 你代码中 prodList.push(...)
但是初始的时候被设置 prodList = 123 而且没有任何提示, 那运行 push(...) 肯定要报未知方法的错误
这种问题在自由的 JavaScript 世界很普遍,如果在 编译 运行 两个阶段都没提示的话,排错是很麻烦的,全靠经验和对业务的熟悉 (陈年老项目又没文档简直是地狱)
所以我们要把问题消灭在萌芽中,就是申明对象的时候同时把类型也定义掉了, react 的自带方案是 PropTypes 组件
4. 常见报错(类型检查)
类型不一致报错:
1 2 3 4 5 6 7 8 9 10 11 12
| class Greeting extends React.Component { render() { return <h1>Hello, {this.props.name}</h1> } } Greeting.propTypes = { name: PropTypes.string }
<div> <Greeting name={123} /> </div>
|

限制单个子代:
1 2 3 4 5 6 7 8 9 10 11 12 13
| const OnlyOneChild = props => { const children = props.children return <div>{children}</div> } OnlyOneChild.propTypes = { children: PropTypes.element.isRequired }
<OnlyOneChild> <h3>第一个子节点</h3> <h3>第二个子节点</h3> </OnlyOneChild>
|

参考资料
prop-types NPM
React 快速上手 - 10 类型检查 PropTypes segmentfault