{"id":207358,"date":"2020-12-21T10:09:46","date_gmt":"2020-12-21T02:09:46","guid":{"rendered":"https:\/\/gulass.cn\/?p=207358"},"modified":"2020-12-21T10:09:46","modified_gmt":"2020-12-21T02:09:46","slug":"react-study-rendering-2","status":"publish","type":"post","link":"https:\/\/gulass.cn\/react-study-rendering-2.html","title":{"rendered":"React\u5b66\u4e60\u7b14\u8bb0-\u5143\u7d20\u6e32\u67d3"},"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

\"\"
\n\u5143\u7d20\u662f\u6784\u6210 React \u5e94\u7528\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u5b83\u7528\u4e8e\u63cf\u8ff0\u5c4f\u5e55\u4e0a\u8f93\u51fa\u7684\u5185\u5bb9\u3002<\/p>\n

\r\nconst element = <h1>Hello, world!<\/h1>;<\/pre>\n

\u4e0e\u6d4f\u89c8\u5668\u7684 DOM \u5143\u7d20\u4e0d\u540c\uff0cReact \u5f53\u4e2d\u7684\u5143\u7d20\u4e8b\u5b9e\u4e0a\u662f\u666e\u901a\u7684\u5bf9\u8c61\uff0cReact DOM \u53ef\u4ee5\u786e\u4fdd \u6d4f\u89c8\u5668 DOM \u7684\u6570\u636e\u5185\u5bb9\u4e0e React \u5143\u7d20\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n

\u5c06\u5143\u7d20\u6e32\u67d3\u5230 DOM \u4e2d<\/strong><\/div>\n

\u9996\u5148\u6211\u4eec\u5728\u4e00\u4e2a HTML \u9875\u9762\u4e2d\u6dfb\u52a0\u4e00\u4e2a id=\"example\" \u7684 <div>:<\/p>\n

\r\n<div id=\"example\"><\/div><\/pre>\n

\u5728\u6b64 div \u4e2d\u7684\u6240\u6709\u5185\u5bb9\u90fd\u5c06\u7531 React DOM \u6765\u7ba1\u7406\uff0c\u6240\u4ee5\u6211\u4eec\u5c06\u5176\u79f0\u4e3a \"\u6839\" DOM \u8282\u70b9\u3002<\/p>\n

\u6211\u4eec\u7528 React \u5f00\u53d1\u5e94\u7528\u65f6\u4e00\u822c\u53ea\u4f1a\u5b9a\u4e49\u4e00\u4e2a\u6839\u8282\u70b9\u3002\u4f46\u5982\u679c\u4f60\u662f\u5728\u4e00\u4e2a\u5df2\u6709\u7684\u9879\u76ee\u5f53\u4e2d\u5f15\u5165 React \u7684\u8bdd\uff0c\u4f60\u53ef\u80fd\u4f1a\u9700\u8981\u5728\u4e0d\u540c\u7684\u90e8\u5206\u5355\u72ec\u5b9a\u4e49 React \u6839\u8282\u70b9\u3002<\/p>\n

\u8981\u5c06React\u5143\u7d20\u6e32\u67d3\u5230\u6839DOM\u8282\u70b9\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u628a\u5b83\u4eec\u90fd\u4f20\u9012\u7ed9 ReactDOM.render() \u7684\u65b9\u6cd5\u6765\u5c06\u5176\u6e32\u67d3\u5230\u9875\u9762\u4e0a\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nconst element = <h1>Hello, world!<\/h1>;\r\nReactDOM.render(\r\n    element,\r\n    document.getElementById('example')\r\n);<\/pre>\n
\u66f4\u65b0\u5143\u7d20\u6e32\u67d3<\/strong><\/div>\n

React \u5143\u7d20\u90fd\u662f\u4e0d\u53ef\u53d8\u7684\u3002\u5f53\u5143\u7d20\u88ab\u521b\u5efa\u4e4b\u540e\uff0c\u4f60\u662f\u65e0\u6cd5\u6539\u53d8\u5176\u5185\u5bb9\u6216\u5c5e\u6027\u7684\u3002<\/p>\n

\u76ee\u524d\u66f4\u65b0\u754c\u9762\u7684\u552f\u4e00\u529e\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5b83\u4f20\u5165 ReactDOM.render() \u65b9\u6cd5\uff1a<\/p>\n

\u6765\u770b\u4e00\u4e0b\u8fd9\u4e2a\u8ba1\u65f6\u5668\u7684\u4f8b\u5b50:<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nfunction tick() {\r\n  const element = (\r\n    <div>\r\n      <h1>Hello, world!<\/h1>\r\n      <h2>\u73b0\u5728\u662f {new Date().toLocaleTimeString()}.<\/h2>\r\n    <\/div>\r\n  );\r\n  ReactDOM.render(\r\n    element,\r\n    document.getElementById('example')\r\n  );\r\n}\r\n \r\nsetInterval(tick, 1000);<\/pre>\n

\u4ee5\u4e0a\u5b9e\u4f8b\u901a\u8fc7 setInterval() \u65b9\u6cd5\uff0c\u6bcf\u79d2\u949f\u8c03\u7528\u4e00\u6b21 ReactDOM.render()\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u5c06\u8981\u5c55\u793a\u7684\u90e8\u5206\u5c01\u88c5\u8d77\u6765\uff0c\u4ee5\u4e0b\u5b9e\u4f8b\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u8868\u793a\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nfunction Clock(props) {\r\n  return (\r\n    <div>\r\n      <h1>Hello, world!<\/h1>\r\n      <h2>\u73b0\u5728\u662f {props.date.toLocaleTimeString()}.<\/h2>\r\n    <\/div>\r\n  );\r\n}\r\n \r\nfunction tick() {\r\n  ReactDOM.render(\r\n    <Clock date={new Date()} \/>,\r\n    document.getElementById('example')\r\n  );\r\n}\r\n \r\nsetInterval(tick, 1000);\r\n<\/pre>\n

\u9664\u4e86\u51fd\u6570\u5916\u6211\u4eec\u8fd8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a React.Component \u7684 ES6 \u7c7b\uff0c\u8be5\u7c7b\u5c01\u88c5\u4e86\u8981\u5c55\u793a\u7684\u5143\u7d20\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u5728 render() \u65b9\u6cd5\u4e2d\uff0c\u9700\u8981\u4f7f\u7528 this.props \u66ff\u6362 props\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nclass Clock extends React.Component {\r\n  render() {\r\n    return (\r\n      <div>\r\n        <h1>Hello, world!<\/h1>\r\n        <h2>\u73b0\u5728\u662f {this.props.date.toLocaleTimeString()}.<\/h2>\r\n      <\/div>\r\n    );\r\n  }\r\n}\r\n \r\nfunction tick() {\r\n  ReactDOM.render(\r\n    <Clock date={new Date()} \/>,\r\n    document.getElementById('example')\r\n  );\r\n}\r\n \r\nsetInterval(tick, 1000);<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5143\u7d20\u662f\u6784\u6210 React \u5e94\u7528\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u5b83\u7528\u4e8e\u63cf\u8ff0\u5c4f\u5e55\u4e0a\u8f93\u51fa\u7684\u5185\u5bb9\u3002 const element = < […]<\/p>\n","protected":false},"author":1470,"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-207358","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/207358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=207358"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/207358\/revisions"}],"predecessor-version":[{"id":207362,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/207358\/revisions\/207362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/207359"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=207358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=207358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=207358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}