{"id":143,"date":"2019-01-01T23:19:37","date_gmt":"2019-01-01T15:19:37","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=143"},"modified":"2020-06-20T23:13:47","modified_gmt":"2020-06-20T15:13:47","slug":"react%e7%bb%84%e4%bb%b6%e6%87%92%e5%8a%a0%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2019\/01\/01\/react%e7%bb%84%e4%bb%b6%e6%87%92%e5%8a%a0%e8%bd%bd\/","title":{"rendered":"REACT\u7ec4\u4ef6\u61d2\u52a0\u8f7d"},"content":{"rendered":"<hr \/>\n<p>\u5143\u65e6\u4e86\u3002&#x1f601;<\/p>\n<blockquote><p>\n  react 16.6\u5f00\u59cb\u52a0\u5165\u4e86\u4ee3\u7801\u62c6\u5206\u548c\u61d2\u52a0\u8f7d\u529f\u80fd\uff0c\u51cf\u8f7b\u4e86\u5f88\u591a\u5f00\u53d1\u8005\u7684\u5de5\u4f5c\u3002\n<\/p><\/blockquote>\n<h4>\u5b89\u88c5<\/h4>\n<pre><code class=\"language-js line-numbers\">$ npm install --save react react-dom\n    or\n$ yarn add react react-dom\n<\/code><\/pre>\n<p>\u5728\u8981\u5904\u7406\u5ef6\u8fdf\u52a0\u8f7d\u7684\u7ec4\u4ef6\u4e2d\u5bfc\u5165Suspense\u5e76lazy:<\/p>\n<pre><code class=\"language-js line-numbers\">import React, { lazy, Suspense } from 'react';\n<\/code><\/pre>\n<h4>\u4ee3\u7801\u62c6\u5206<\/h4>\n<p>\u867d\u7136Suspense\u5e76\u4e14lazy\u5141\u8bb8\u5feb\u901f\u521b\u5efa\u5ef6\u8fdf\u52a0\u8f7d\u7ec4\u4ef6\uff0c\u4f46\u5982\u679c\u5c06\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u5230Webpack\u8fd9\u6837\u7684\u5355\u4e2a\u6587\u4ef6\u4e2d\u5e76\u6ca1\u6709\u4ec0\u4e48\u597d\u5904\u3002<\/p>\n<p><em>Webpack\u4e0d\u5728\u672c\u6587\u7684\u8ba8\u8bba\u8303\u56f4\uff0c\u53ef\u4ee5\u5728Webpack\u6587\u6863\u7684<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/webpack.js.org\/plugins\/commons-chunk-plugin\/\">\u4ee3\u7801\u62c6\u5206<\/a>\u4e00\u7ae0\u4e2d\u67e5\u9605\u3002<\/em><\/p>\n<h4>lazy \u65b9\u6cd5<\/h4>\n<p>lazy\u65b9\u6cd5\u5c06\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-js line-numbers\">const SomeComponent = lazy(() =&gt; import('.\/SomeComponent');\n<\/code><\/pre>\n<p>\u56e0\u4e3aimport\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5185\u90e8\uff0c\u6240\u4ee5\u5728\u5b9e\u9645\u4f7f\u7528\u7ec4\u4ef6\u4e4b\u524d\u4e0d\u4f1a\u52a0\u8f7d\u7ec4\u4ef6\u3002<\/p>\n<h4>Suspense \u5c5e\u6027<\/h4>\n<p>React.Suspense \u5141\u8bb8\u5f00\u53d1\u8005\u5c06\u7ec4\u4ef6\u5305\u88c5\u8d77\u6765\u5e76\u6307\u5b9afallback\u5728\u52a0\u8f7d\u65f6\u8981\u663e\u793a\u7684\u7ec4\u4ef6\uff1a<\/p>\n<pre><code class=\"language-js line-numbers\">&lt;Suspense fallback={&lt;div&gt;Loading...&lt;\/div&gt;}&gt;\n  &lt;SomeComponent \/&gt;\n&lt;\/Suspense&gt;\n<\/code><\/pre>\n<p>\u5f53\u52a0\u8f7d\u5927\u578b\u7ec4\u4ef6\u6216\u5bfc\u5165\u5927\u578b\u5e93\uff08\u5982react-pdf\u6216react-draft-wysiwyg\uff09\u65f6\uff0c\u8fd9\u5c31\u5f88\u6709\u7528\u4e86\u3002<\/p>\n<p>\u5373\u4f7f\u7ec4\u4ef6\u4e0d\u5e38\u7528\uff0c\u5ef6\u8fdf\u52a0\u8f7d\u4e5f\u53ef\u4ee5\u8ba9\u7528\u6237\u6709\u6700\u4f73\u7684\u7f51\u7edc\u8fde\u63a5\u4f53\u9a8c\u3002<\/p>\n<h4>\u4f7f\u7528<\/h4>\n<p>\u521b\u5efa\u4e24\u4e2a\u6587\u4ef6\uff1a\u4e00\u4e2a\u662f\u6211\u4eec\u6b63\u5e38\u7684\u7ec4\u4ef6\uff0c\u4e00\u4e2a\u662f\u5ef6\u8fdf\u52a0\u8f7d\u7ec4\u4ef6\u3002<\/p>\n<p>\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u61d2\u52a0\u8f7d\u7ec4\u4ef6\uff1a<\/p>\n<pre><code class=\"language-js line-numbers\">import React from \"react\";\n\nfunction AllLogo() {\n  const imageSource = \"https:\/\/example.com\"\n    + \"\/imgs\"\n    + \"\/all-logo.png\";\n\n  return (\n    &lt;img\n      alt=\"All Logo\"\n      src={imageSource}\n      width=\"250\"\n    \/&gt;\n  );\n}\n\nexport default AllLogo;\n<\/code><\/pre>\n<p>\u7136\u540e\u662f\u4e00\u4e9b\u72b6\u6001\u903b\u8f91\u5904\u7406\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u624b\u52a8\u89e6\u53d1\u7ec4\u4ef6\u7684\u52a0\u8f7d\uff1a<\/p>\n<pre><code class=\"language-js line-numbers\">import React, { Component, Fragment, lazy, Suspense } from \"react\";\nimport { render } from \"react-dom\";\n\nimport \".\/styles.scss\";\n\nconst AllLogo = lazy(() =&gt; import(\".\/AllLogo\"));\n\nclass App extends Component {\n  constructor(props) {\n    super(props);\n\n    this.state = {\n      showLogo: false\n    };\n  }\n\n  onClick = () =&gt; {\n    this.setState({ showLogo: !this.state.showLogo });\n  };\n\n  render() {\n    return (\n      &lt;div className=\"App\"&gt;\n        &lt;h1&gt;REACT \u61d2\u52a0\u8f7d\u7279\u6027&lt;\/h1&gt;\n        &lt;div&gt;\n          &lt;button onClick={this.onClick}&gt;\n            {this.state.showLogo &amp;&amp;\n              &lt;Fragment&gt;\u9690\u85cf&lt;\/Fragment&gt;\n            }\n            {!this.state.showLogo &amp;&amp; (\n              &lt;Fragment&gt;\u663e\u793a&lt;\/Fragment&gt;\n            )}\n          &lt;\/button&gt;\n        &lt;\/div&gt;\n        &lt;hr \/&gt;\n        {this.state.showLogo &amp;&amp; (\n          &lt;Suspense fallback={&lt;div&gt;Loading...&lt;\/div&gt;}&gt;\n            &lt;AllLogo \/&gt;\n          &lt;\/Suspense&gt;\n        )}\n      &lt;\/div&gt;\n    );\n  }\n}\n\nconst container = document.createElement(\"div\");\ndocument.body.appendChild(container);\nrender(&lt;App \/&gt;, container);\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4fee\u6539\u6b63\u5728\u5f00\u53d1\u6216\u8005\u73b0\u6709\u7684\u9879\u76ee\u4e0d\u4f1a\u6709\u592a\u591a\u6539\u52a8\uff0c\u7279\u522b\u662f\u5df2\u7ecf\u5c06\u7ec4\u4ef6\u62c6\u5206\u7684\u9879\u76ee\u3002<\/p>\n<h4>\u4f18\u70b9<\/h4>\n<p>\u5f15\u5165React.lazy()\u548cReact.Suspense\uff0c\u53ef\u4ee5\u51cf\u5c11\u7f16\u5199Web\u7a0b\u5e8f\u7684\u4ee3\u7801\u91cf\u3002<\/p>\n<p>React\u672a\u6765\u7248\u672c\u5c06\u5728Suspense\u4e2d\u52a0\u5165\u6570\u636e\u83b7\u53d6\u529f\u80fd\uff0c\u90a3\u4e48Suspense\u5c31\u53ef\u4ee5\u7b49\u5f85AJAX\u200b\u200b\u8bf7\u6c42\u6570\u636e\u540e\u5728\u52a0\u8f7d\u65f6\u663e\u793a\u7ec4\u4ef6\u3002<\/p>\n<p>\u4f46\u8981\u6ce8\u610f\uff0cSuspense\u8fd8\u4e0d\u80fd\u5728\u670d\u52a1\u5668\u7aef\u6e32\u67d3\u7684\u65f6\u5019\u4f7f\u7528\u3002<\/p>\n\n<p>code enjoy! &#x1f92a;&#x1f92a;&#x1f92a;<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/indeex.cc\/\">https:\/\/indeex.cc<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u5143\u65e6\u4e86\u3002&#x1f601; react 16.6\u5f00\u59cb\u52a0\u5165\u4e86\u4ee3\u7801\u62c6\u5206\u548c\u61d2\u52a0\u8f7d\u529f\u80fd\uff0c\u51cf\u8f7b\u4e86\u5f88\u591a\u5f00\u53d1\u8005\u7684\u5de5<a href=\"https:\/\/blog.indeex.club\/index.php\/2019\/01\/01\/react%e7%bb%84%e4%bb%b6%e6%87%92%e5%8a%a0%e8%bd%bd\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/143"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}