{"id":4586,"date":"2023-01-04T16:32:07","date_gmt":"2023-01-04T08:32:07","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4586"},"modified":"2023-01-04T16:32:07","modified_gmt":"2023-01-04T08:32:07","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react%e7%b5%84%e4%bb%b6%e7%b9%ab%e7%b5%90%e8%b3%87%e6%96%99","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react%e7%b5%84%e4%bb%b6%e7%b9%ab%e7%b5%90%e8%b3%87%e6%96%99\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528React\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, \u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599\"><\/p>\n<h1>React \u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599<\/h1>\n<p>React \u662f\u4e00\u500b\u7531 Facebook \u6240\u958b\u767c\u7684 JavaScript \u51fd\u5f0f\u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5efa\u7acb\u4e92\u52d5\u5f0f\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u3002React \u7684\u6838\u5fc3\u6982\u5ff5\u662f\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u8cc7\u6599\u8207\u7d44\u4ef6\u7e6b\u7d50\uff0c\u4ee5\u4fbf\u5728\u8cc7\u6599\u6539\u8b8a\u6642\uff0c\u7d44\u4ef6\u4e5f\u6703\u8ddf\u8457\u6539\u8b8a\u3002<\/p>\n<p>React \u7684\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599\u7684\u6982\u5ff5\u662f\u5efa\u7acb\u5728\u4e00\u500b\u53eb\u505a\u300c\u865b\u64ec DOM\u300d\u7684\u6982\u5ff5\u4e0a\uff0c\u5b83\u662f\u4e00\u500b JavaScript \u7269\u4ef6\uff0c\u53ef\u4ee5\u7528\u4f86\u6a21\u64ec\u7db2\u9801\u4e0a\u7684 DOM \u7d50\u69cb\u3002\u7576\u8cc7\u6599\u6539\u8b8a\u6642\uff0cReact \u6703\u6bd4\u8f03\u65b0\u820a\u7684\u865b\u64ec DOM\uff0c\u4e26\u4e14\u53ea\u66f4\u65b0\u6539\u8b8a\u7684\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6574\u500b\u7db2\u9801\u3002<\/p>\n<p>React \u7684\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599\u7684\u6982\u5ff5\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5efa\u7acb\u4e92\u52d5\u5f0f\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u6b21\u8cc7\u6599\u6539\u8b8a\u6642\u90fd\u8981\u91cd\u65b0\u6e32\u67d3\u6574\u500b\u7db2\u9801\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 React \u7684\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599<\/h2>\n<p>\u8981\u4f7f\u7528 React \u7684\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599\uff0c\u958b\u767c\u8005\u9700\u8981\u5148\u5efa\u7acb\u4e00\u500b React \u7d44\u4ef6\uff0c\u4e26\u4e14\u5c07\u8cc7\u6599\u50b3\u5165\u7d44\u4ef6\u4e2d\u3002React \u7684\u7d44\u4ef6\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 class \u6216\u662f\u51fd\u5f0f\u4f86\u5efa\u7acb\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528 props \u4f86\u50b3\u5165\u8cc7\u6599\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass MyComponent extends React.Component {\n  render() {\n    return (\n      <div>\n        {this.props.data}\n      <\/div>\n    );\n  }\n}\n\nReactDOM.render(\n  <MyComponent data=\"Hello World!\" \/>,\n  document.getElementById('root')\n);\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5efa\u7acb\u4e86\u4e00\u500b\u540d\u70ba MyComponent \u7684 React \u7d44\u4ef6\uff0c\u4e26\u4e14\u5c07\u4e00\u500b\u5b57\u4e32\u8cc7\u6599\u50b3\u5165\u7d44\u4ef6\u4e2d\uff0c\u7136\u5f8c\u5728\u7d44\u4ef6\u7684 render \u65b9\u6cd5\u4e2d\u5c07\u8cc7\u6599\u986f\u793a\u51fa\u4f86\u3002<\/p>\n<p>\u7576\u8cc7\u6599\u6539\u8b8a\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 React \u7684 setState \u65b9\u6cd5\u4f86\u66f4\u65b0\u8cc7\u6599\uff0c\u4e26\u4e14\u8b93 React \u81ea\u52d5\u66f4\u65b0\u7d44\u4ef6\uff1a<\/p>\n<pre class=\"brush: javascript\">\nclass MyComponent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = {\n      data: props.data\n    };\n  }\n\n  updateData() {\n    this.setState({\n      data: 'Hello World!'\n    });\n  }\n\n  render() {\n    return (\n      <div>\n        {this.state.data}\n      <\/div>\n    );\n  }\n}\n\nReactDOM.render(\n  <MyComponent data=\"Hello!\" \/>,\n  document.getElementById('root')\n);\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5c07\u8cc7\u6599\u5b58\u653e\u5728 React \u7d44\u4ef6\u7684 state \u4e2d\uff0c\u4e26\u4e14\u4f7f\u7528 setState \u65b9\u6cd5\u4f86\u66f4\u65b0\u8cc7\u6599\uff0c\u7576\u8cc7\u6599\u6539\u8b8a\u6642\uff0cReact \u6703\u81ea\u52d5\u66f4\u65b0\u7d44\u4ef6\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>React \u7684\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599\u7684\u6982\u5ff5\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5efa\u7acb\u4e92\u52d5\u5f0f\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u6b21\u8cc7\u6599\u6539\u8b8a\u6642\u90fd\u8981\u91cd\u65b0\u6e32\u67d3\u6574\u500b\u7db2\u9801\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 React \u7684 props \u548c setState \u65b9\u6cd5\u4f86\u5c07\u8cc7\u6599\u50b3\u5165\u7d44\u4ef6\u4e2d\uff0c\u4e26\u4e14\u5728\u8cc7\u6599\u6539\u8b8a\u6642\u8b93 React \u81ea\u52d5\u66f4\u65b0\u7d44\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528React\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u7684\u958b\u767c\u51fa\u66f4\u5177\u6709\u5f48\u6027\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eReact\u7d44\u4ef6\u7e6b\u7d50\u8cc7\u6599\u7684\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u5efa\u7acb\u66f4\u5177\u5f48\u6027\u7684\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-4586","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1bY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4586"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4586\/revisions"}],"predecessor-version":[{"id":4587,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4586\/revisions\/4587"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}