{"id":207530,"date":"2020-12-25T09:13:09","date_gmt":"2020-12-25T01:13:09","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=207530"},"modified":"2020-12-21T10:14:38","modified_gmt":"2020-12-21T02:14:38","slug":"react-study-props","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/react-study-props.html","title":{"rendered":"React\u5b66\u4e60\u7b14\u8bb0-Props"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nReact \u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u7528\u6237\u754c\u9762\u7684 JAVASCRIPT \u5e93\u3002React \u4e3b\u8981\u7528\u4e8e\u6784\u5efaUI\uff0c\u5f88\u591a\u4eba\u8ba4\u4e3a React \u662f MVC \u4e2d\u7684 V\uff08\u89c6\u56fe\uff09\u3002React \u8d77\u6e90\u4e8e Facebook \u7684\u5185\u90e8\u9879\u76ee\uff0c\u7528\u6765\u67b6\u8bbe Instagram \u7684\u7f51\u7ad9\uff0c\u5e76\u4e8e 2013 \u5e74 5 \u6708\u5f00\u6e90\u3002React \u62e5\u6709\u8f83\u9ad8\u7684\u6027\u80fd\uff0c\u4ee3\u7801\u903b\u8f91\u975e\u5e38\u7b80\u5355\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u5df2\u5f00\u59cb\u5173\u6ce8\u548c\u4f7f\u7528\u5b83\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nstate \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\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6709\u4e9b\u5bb9\u5668\u7ec4\u4ef6\u9700\u8981\u5b9a\u4e49 state \u6765\u66f4\u65b0\u548c\u4fee\u6539\u6570\u636e\u3002 \u800c\u5b50\u7ec4\u4ef6\u53ea\u80fd\u901a\u8fc7 props \u6765\u4f20\u9012\u6570\u636e\u3002<\/p>\n

\u4f7f\u7528 Props<\/strong><\/div>\n

\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528 props\uff1a<\/p>\n

React \u5b9e\u4f8b<\/p>\n

\r\nfunction HelloMessage(props) {\r\n    return <h1>Hello {props.name}!<\/h1>;\r\n}\r\n \r\nconst element = <HelloMessage name=\"Runoob\"\/>;\r\n \r\nReactDOM.render(\r\n    element,\r\n    document.getElementById('example')\r\n);\r\n<\/pre>\n

\u5b9e\u4f8b\u4e2d name \u5c5e\u6027\u901a\u8fc7 props.name \u6765\u83b7\u53d6\u3002<\/p>\n

\u9ed8\u8ba4 Props<\/strong><\/div>\n

\u4f60\u53ef\u4ee5\u901a\u8fc7\u7ec4\u4ef6\u7c7b\u7684 defaultProps \u5c5e\u6027\u4e3a props \u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n

React \u5b9e\u4f8b<\/p>\n

\r\nclass HelloMessage extends React.Component {\r\n  render() {\r\n    return (\r\n      <h1>Hello, {this.props.name}<\/h1>\r\n    );\r\n  }\r\n}\r\n \r\nHelloMessage.defaultProps = {\r\n  name: 'Runoob'\r\n};\r\n \r\nconst element = <HelloMessage\/>;\r\n \r\nReactDOM.render(\r\n  element,\r\n  document.getElementById('example')\r\n);\r\n<\/pre>\n
State \u548c Props<\/strong><\/div>\n

\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5728\u5e94\u7528\u4e2d\u7ec4\u5408\u4f7f\u7528 state \u548c props \u3002\u6211\u4eec\u53ef\u4ee5\u5728\u7236\u7ec4\u4ef6\u4e2d\u8bbe\u7f6e state\uff0c \u5e76\u901a\u8fc7\u5728\u5b50\u7ec4\u4ef6\u4e0a\u4f7f\u7528 props \u5c06\u5176\u4f20\u9012\u5230\u5b50\u7ec4\u4ef6\u4e0a\u3002\u5728 render \u51fd\u6570\u4e2d, \u6211\u4eec\u8bbe\u7f6e name \u548c site \u6765\u83b7\u53d6\u7236\u7ec4\u4ef6\u4f20\u9012\u8fc7\u6765\u7684\u6570\u636e\u3002<\/p>\n

React \u5b9e\u4f8b<\/p>\n

\r\nclass WebSite extends React.Component {\r\n  constructor() {\r\n      super();\r\n \r\n      this.state = {\r\n        name: \"Linux\u5c31\u8be5\u8fd9\u4e48\u5b66\",\r\n        site: \"https:\/\/lrxjmw.cn\"\r\n      }\r\n    }\r\n  render() {\r\n    return (\r\n      <div>\r\n        <Name name={this.state.name} \/>\r\n        <Link site={this.state.site} \/>\r\n      <\/div>\r\n    );\r\n  }\r\n}\r\n \r\n \r\n \r\nclass Name extends React.Component {\r\n  render() {\r\n    return (\r\n      <h1>{this.props.name}<\/h1>\r\n    );\r\n  }\r\n}\r\n \r\nclass Link extends React.Component {\r\n  render() {\r\n    return (\r\n      <a href={this.props.site}>\r\n        {this.props.site}\r\n      <\/a>\r\n    );\r\n  }\r\n}\r\n \r\nReactDOM.render(\r\n  <WebSite \/>,\r\n  document.getElementById('example')\r\n);\r\n<\/pre>\n
Props \u9a8c\u8bc1<\/strong><\/div>\n

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>\n

Props \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}]}}