React.PropTypes \u5728 React v15.5 \u7248\u672c\u540e\u5df2\u7ecf\u79fb\u5230\u4e86 prop-types \u5e93\u3002<\/p>\n
\r\n<script src=\"https:\/\/cdn.bootcss.com\/prop-types\/15.6.1\/prop-types.js\"><\/script><\/pre>\nProps \u9a8c\u8bc1\u4f7f\u7528 propTypes\uff0c\u5b83\u53ef\u4ee5\u4fdd\u8bc1\u6211\u4eec\u7684\u5e94\u7528\u7ec4\u4ef6\u88ab\u6b63\u786e\u4f7f\u7528\uff0cReact.PropTypes \u63d0\u4f9b\u5f88\u591a\u9a8c\u8bc1\u5668 (validator) \u6765\u9a8c\u8bc1\u4f20\u5165\u6570\u636e\u662f\u5426\u6709\u6548\u3002\u5f53\u5411 props \u4f20\u5165\u65e0\u6548\u6570\u636e\u65f6\uff0cJavaScript \u63a7\u5236\u53f0\u4f1a\u629b\u51fa\u8b66\u544a\u3002<\/p>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u521b\u5efa\u4e00\u4e2a Mytitle \u7ec4\u4ef6\uff0c\u5c5e\u6027 title \u662f\u5fc5\u987b\u7684\u4e14\u662f\u5b57\u7b26\u4e32\uff0c\u975e\u5b57\u7b26\u4e32\u7c7b\u578b\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32 \uff1a<\/p>\n
React 16.4 \u5b9e\u4f8b<\/p>\n
\r\nvar title = \"Linux\u5c31\u8be5\u8fd9\u4e48\u5b66\";\r\n\/\/ var title = 123;\r\nclass MyTitle extends React.Component {\r\n render() {\r\n return (\r\n <h1>Hello, {this.props.title}<\/h1>\r\n );\r\n }\r\n}\r\n \r\nMyTitle.propTypes = {\r\n title: PropTypes.string\r\n};\r\nReactDOM.render(\r\n <MyTitle title={title} \/>,\r\n document.getElementById('example')\r\n);\r\n\r\n\r\nReact 15.4 \u5b9e\u4f8b\r\nvar title = \"Linux\u5c31\u8be5\u8fd9\u4e48\u5b66\";\r\n\/\/ var title = 123;\r\nvar MyTitle = React.createClass({\r\n propTypes: {\r\n title: React.PropTypes.string.isRequired,\r\n },\r\n \r\n render: function() {\r\n return <h1> {this.props.title} <\/h1>;\r\n }\r\n});\r\nReactDOM.render(\r\n <MyTitle title={title} \/>,\r\n document.getElementById('example')\r\n);<\/pre>\n\u66f4\u591a\u9a8c\u8bc1\u5668\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n
\r\nMyComponent.propTypes = {\r\n \/\/ \u53ef\u4ee5\u58f0\u660e prop \u4e3a\u6307\u5b9a\u7684 JS \u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u9ed8\u8ba4\u60c5\u51b5\uff0c\u8fd9\u4e9b\u6570\u636e\u662f\u53ef\u9009\u7684\r\n optionalArray: React.PropTypes.array,\r\n optionalBool: React.PropTypes.bool,\r\n optionalFunc: React.PropTypes.func,\r\n optionalNumber: React.PropTypes.number,\r\n optionalObject: React.PropTypes.object,\r\n optionalString: React.PropTypes.string,\r\n \r\n \/\/ \u53ef\u4ee5\u88ab\u6e32\u67d3\u7684\u5bf9\u8c61 numbers, strings, elements \u6216 array\r\n optionalNode: React.PropTypes.node,\r\n \r\n \/\/ React \u5143\u7d20\r\n optionalElement: React.PropTypes.element,\r\n \r\n \/\/ \u7528 JS \u7684 instanceof \u64cd\u4f5c\u7b26\u58f0\u660e prop \u4e3a\u7c7b\u7684\u5b9e\u4f8b\u3002\r\n optionalMessage: React.PropTypes.instanceOf(Message),\r\n \r\n \/\/ \u7528 enum \u6765\u9650\u5236 prop \u53ea\u63a5\u53d7\u6307\u5b9a\u7684\u503c\u3002\r\n optionalEnum: React.PropTypes.oneOf(['News', 'Photos']),\r\n \r\n \/\/ \u53ef\u4ee5\u662f\u591a\u4e2a\u5bf9\u8c61\u7c7b\u578b\u4e2d\u7684\u4e00\u4e2a\r\n optionalUnion: React.PropTypes.oneOfType([\r\n React.PropTypes.string,\r\n React.PropTypes.number,\r\n React.PropTypes.instanceOf(Message)\r\n ]),\r\n \r\n \/\/ \u6307\u5b9a\u7c7b\u578b\u7ec4\u6210\u7684\u6570\u7ec4\r\n optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),\r\n \r\n \/\/ \u6307\u5b9a\u7c7b\u578b\u7684\u5c5e\u6027\u6784\u6210\u7684\u5bf9\u8c61\r\n optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),\r\n \r\n \/\/ \u7279\u5b9a shape \u53c2\u6570\u7684\u5bf9\u8c61\r\n optionalObjectWithShape: React.PropTypes.shape({\r\n color: React.PropTypes.string,\r\n fontSize: React.PropTypes.number\r\n }),\r\n \r\n \/\/ \u4efb\u610f\u7c7b\u578b\u52a0\u4e0a `isRequired` \u6765\u4f7f prop \u4e0d\u53ef\u7a7a\u3002\r\n requiredFunc: React.PropTypes.func.isRequired,\r\n \r\n \/\/ \u4e0d\u53ef\u7a7a\u7684\u4efb\u610f\u7c7b\u578b\r\n requiredAny: React.PropTypes.any.isRequired,\r\n \r\n \/\/ \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\u3002\u5982\u679c\u9a8c\u8bc1\u5931\u8d25\u9700\u8981\u8fd4\u56de\u4e00\u4e2a Error \u5bf9\u8c61\u3002\u4e0d\u8981\u76f4\u63a5\u4f7f\u7528 `console.warn` \u6216\u629b\u5f02\u5e38\uff0c\u56e0\u4e3a\u8fd9\u6837 `oneOfType` \u4f1a\u5931\u6548\u3002\r\n customProp: function(props, propName, componentName) {\r\n if (!\/matchme\/.test(props[propName])) {\r\n return new Error('Validation failed!');\r\n }\r\n }\r\n }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"state \u548c props \u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e props \u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u800c state \u53ef\u4ee5\u6839\u636e\u4e0e\u7528\u6237\u4ea4\u4e92\u6765\u6539\u53d8\u3002 […]<\/p>\n","protected":false},"author":321,"featured_media":207359,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-207530","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=207530"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207530\/revisions"}],"predecessor-version":[{"id":207532,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207530\/revisions\/207532"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/207359"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=207530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=207530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=207530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}