{"id":649,"date":"2025-01-01T18:17:47","date_gmt":"2025-01-01T10:17:47","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=649"},"modified":"2025-01-01T18:17:47","modified_gmt":"2025-01-01T10:17:47","slug":"react%e7%9a%84%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/01\/react%e7%9a%84%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b\/","title":{"rendered":"React\u7684\u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">React \u662f\u4e00\u4e2a\u7531 Facebook \u5f00\u53d1\u7684 JavaScript \u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u6784\u5efa\u7528\u6237\u754c\u9762\uff0c\u5c24\u5176\u9002\u5408\u6784\u5efa\u5355\u9875\u5e94\u7528\uff08SPA\uff09\u3002React \u901a\u8fc7\u58f0\u660e\u5f0f\u7f16\u7a0b\uff0c\u7ec4\u4ef6\u5316\u5f00\u53d1\uff0c\u8ba9\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u6784\u5efa\u590d\u6742\u7684\u524d\u7aef\u5e94\u7528\u3002React \u6700\u65b0\u7248\u672c\uff08\u622a\u81f3 2025 \u5e74\uff09\u7684\u4e3b\u8981\u7279\u6027\u5305\u62ec <strong>\u51fd\u6570\u7ec4\u4ef6<\/strong>\u3001<strong>Hooks API<\/strong>\u3001<strong>Concurrent Mode<\/strong>\uff08\u5e76\u53d1\u6a21\u5f0f\uff09\u3001<strong>Suspense<\/strong> \u7b49\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6211\u5c06\u4e3a\u4f60\u8be6\u7ec6\u8bb2\u89e3 React \u6700\u65b0\u7248\u672c\u7684\u4f7f\u7528\u6559\u7a0b\uff0c\u4ece\u521b\u5efa\u9879\u76ee\u5f00\u59cb\uff0c\u6db5\u76d6\u7ec4\u4ef6\u3001\u72b6\u6001\u7ba1\u7406\u3001\u4e8b\u4ef6\u5904\u7406\u3001Hooks \u7b49\u5185\u5bb9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u5b89\u88c5\u4e0e\u73af\u5883\u8bbe\u7f6e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u4f7f\u7528 React \u8fdb\u884c\u5f00\u53d1\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u5f00\u53d1\u73af\u5883\u3002\u63a8\u8350\u4f7f\u7528 <a href=\"https:\/\/reactjs.org\/docs\/create-a-new-react-app.html\">Create React App<\/a> \u6765\u5feb\u901f\u542f\u52a8\u4e00\u4e2a\u65b0\u7684 React \u9879\u76ee\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 \u5b89\u88c5 Node.js \u548c npm<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">React \u4f9d\u8d56 Node.js \u548c npm\uff08Node.js \u7684\u5305\u7ba1\u7406\u5668\uff09\u3002\u4f60\u53ef\u4ee5\u4ece <a href=\"https:\/\/nodejs.org\/\">Node.js \u5b98\u7f51<\/a> \u4e0b\u8f7d\u5e76\u5b89\u88c5 Node.js\uff0c\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5\u7248\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>node -v\nnpm -v<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 \u4f7f\u7528 Create React App \u521b\u5efa\u9879\u76ee<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Create React App<\/code> \u662f\u5b98\u65b9\u63d0\u4f9b\u7684\u811a\u624b\u67b6\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u521b\u5efa React \u9879\u76ee\u5e76\u914d\u7f6e\u597d\u5f00\u53d1\u73af\u5883\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5 Create React App\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -g create-react-app<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u65b0\u9879\u76ee\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx create-react-app my-app<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\u5e76\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd my-app\nnpm start<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9879\u76ee\u9ed8\u8ba4\u4f1a\u8fd0\u884c\u5728 <code>http:\/\/localhost:3000<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>React \u7ec4\u4ef6\u57fa\u7840<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">React \u662f\u57fa\u4e8e\u7ec4\u4ef6\u7684\u5f00\u53d1\u6a21\u5f0f\uff0c\u6240\u6709\u7684 UI \u90fd\u662f\u7531\u7ec4\u4ef6\u6784\u6210\u7684\u3002\u6bcf\u4e2a\u7ec4\u4ef6\u90fd\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u7c7b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 \u51fd\u6570\u7ec4\u4ef6<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">React \u7684\u51fd\u6570\u7ec4\u4ef6\u662f\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u5c24\u5176\u662f\u5728 React 16.8 \u5f15\u5165\u4e86 Hooks \u540e\uff0c\u51fd\u6570\u7ec4\u4ef6\u53d8\u5f97\u66f4\u52a0\u5f3a\u5927\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ src\/App.js\nimport React from 'react';\n\nfunction App() {\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Hello, React!&lt;\/h1&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4e2d\uff0c<code>App<\/code> \u662f\u4e00\u4e2a\u51fd\u6570\u7ec4\u4ef6\uff0c\u5b83\u8fd4\u56de\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 JSX \u7ed3\u6784\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 \u7c7b\u7ec4\u4ef6\uff08\u65e7\u7248\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u7c7b\u7ec4\u4ef6\u662f React \u7684\u53e6\u4e00\u79cd\u7ec4\u4ef6\u7c7b\u578b\uff0c\u5b83\u5177\u6709\u66f4\u590d\u6742\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u751f\u547d\u5468\u671f\u65b9\u6cd5\u3002\u4e0d\u8fc7\u5728\u6700\u65b0\u7684 React \u4e2d\uff0c\u7c7b\u7ec4\u4ef6\u7684\u4f7f\u7528\u5df2\u7ecf\u51cf\u5c11\uff0c\u63a8\u8350\u4f7f\u7528\u51fd\u6570\u7ec4\u4ef6\u548c Hooks\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { Component } from 'react';\n\nclass App extends Component {\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;h1&gt;Hello, React!&lt;\/h1&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n\nexport default App;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>JSX \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">React \u4f7f\u7528\u4e00\u79cd\u53eb\u505a <strong>JSX<\/strong>\uff08JavaScript XML\uff09\u7684\u8bed\u6cd5\u6765\u63cf\u8ff0\u7ec4\u4ef6\u7684\u7ed3\u6784\u3002JSX \u662f JavaScript \u7684\u4e00\u79cd\u8bed\u6cd5\u6269\u5c55\uff0c\u5141\u8bb8\u5728 JavaScript \u4e2d\u5d4c\u5165 HTML \u7ed3\u6784\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 JSX \u57fa\u672c\u89c4\u5219<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>JSX \u5143\u7d20\u5fc5\u987b\u6709\u4e00\u4e2a\u7236\u5143\u7d20\uff08\u53ef\u4ee5\u662f <code>&lt;div><\/code> \u6216\u5176\u4ed6\u6807\u7b7e\uff09\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u53ef\u4ee5\u5d4c\u5165 JavaScript \u8868\u8fbe\u5f0f\u3002<\/li>\n\n\n\n<li>\u7c7b\u540d\u9700\u8981\u7528 <code>className<\/code>\uff08\u800c\u4e0d\u662f <code>class<\/code>\uff09\u6765\u4ee3\u66ff\uff0c\u907f\u514d\u4e0e JavaScript \u4e2d\u7684\u5173\u952e\u5b57\u51b2\u7a81\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>function App() {\n  const name = \"React\";\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Hello, {name}!&lt;\/h1&gt;\n    &lt;\/div&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>React \u7ec4\u4ef6\u7684\u72b6\u6001\u7ba1\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">React \u7ec4\u4ef6\u7684\u72b6\u6001\u662f\u7528\u4e8e\u5b58\u50a8\u7ec4\u4ef6\u5185\u90e8\u7684\u6570\u636e\u3002\u5f53\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u7ec4\u4ef6\u4f1a\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 \u4f7f\u7528 useState Hook<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">React 16.8 \u5f15\u5165\u4e86 <strong>Hooks<\/strong>\uff0c<code>useState<\/code> \u662f\u6700\u5e38\u7528\u7684 Hook\uff0c\u7528\u4e8e\u58f0\u660e\u548c\u66f4\u65b0\u7ec4\u4ef6\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useState } from 'react';\n\nfunction Counter() {\n  \/\/ useState \u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u72b6\u6001\u503c\uff0c\u7b2c\u4e8c\u4e2a\u662f\u66f4\u65b0\u72b6\u6001\u7684\u51fd\u6570\n  const &#91;count, setCount] = useState(0);\n\n  return (\n    &lt;div&gt;\n      &lt;p&gt;You clicked {count} times&lt;\/p&gt;\n      &lt;button onClick={() =&gt; setCount(count + 1)}&gt;\n        Click me\n      &lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default Counter;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>count<\/code> \u662f\u5f53\u524d\u72b6\u6001\uff0c<code>setCount<\/code> \u662f\u66f4\u65b0\u72b6\u6001\u7684\u51fd\u6570\u3002\u6bcf\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\uff0c<code>setCount<\/code> \u88ab\u8c03\u7528\uff0c\u66f4\u65b0\u72b6\u6001\u5e76\u89e6\u53d1\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 \u72b6\u6001\u7684\u521d\u59cb\u503c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useState<\/code> \u63a5\u53d7\u4e00\u4e2a\u521d\u59cb\u503c\u53c2\u6570\u3002\u5982\u679c\u72b6\u6001\u503c\u662f\u52a8\u6001\u8ba1\u7b97\u7684\uff0c\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const &#91;count, setCount] = useState(() =&gt; computeInitialCount());<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>React \u4e8b\u4ef6\u5904\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">React \u4e8b\u4ef6\u5904\u7406\u4f7f\u7528 camelCase\uff08\u9a7c\u5cf0\u5f0f\u547d\u540d\uff09\u6765\u547d\u540d\u4e8b\u4ef6\uff0c\u800c\u4e0d\u662f HTML \u7684\u5c0f\u5199\u547d\u540d\u65b9\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function App() {\n  const handleClick = () =&gt; {\n    alert('Button clicked!');\n  };\n\n  return (\n    &lt;button onClick={handleClick}&gt;\n      Click me\n    &lt;\/button&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>onClick<\/code> \u662f\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668\u3002<\/li>\n\n\n\n<li>React \u7684\u4e8b\u4ef6\u7cfb\u7edf\u662f\u57fa\u4e8e\u5408\u6210\u4e8b\u4ef6\u7684\uff0c\u5b83\u8de8\u6d4f\u89c8\u5668\u4e00\u81f4\u6027\u5730\u5904\u7406\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u7ec4\u4ef6\u4e4b\u95f4\u7684\u901a\u4fe1<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">React \u7ec4\u4ef6\u901a\u5e38\u662f\u5c42\u7ea7\u5316\u7684\uff0c\u7236\u7ec4\u4ef6\u53ef\u4ee5\u901a\u8fc7 <strong>props<\/strong> \u5411\u5b50\u7ec4\u4ef6\u4f20\u9012\u6570\u636e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6.1 \u7236\u7ec4\u4ef6\u5411\u5b50\u7ec4\u4ef6\u4f20\u9012\u6570\u636e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ParentComponent.js\nimport React from 'react';\nimport ChildComponent from '.\/ChildComponent';\n\nfunction ParentComponent() {\n  return &lt;ChildComponent name=\"React\" \/&gt;;\n}\n\nexport default ParentComponent;\n\n\/\/ ChildComponent.js\nimport React from 'react';\n\nfunction ChildComponent({ name }) {\n  return &lt;h1&gt;Hello, {name}!&lt;\/h1&gt;;\n}\n\nexport default ChildComponent;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>ParentComponent<\/code> \u5411 <code>ChildComponent<\/code> \u4f20\u9012\u4e86\u4e00\u4e2a <code>name<\/code> \u5c5e\u6027\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u4f7f\u7528 Effect Hook\uff1auseEffect<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useEffect<\/code> \u662f React \u7684\u4e00\u4e2a Hook\uff0c\u7528\u4e8e\u5904\u7406\u526f\u4f5c\u7528\uff0c\u4f8b\u5982\u6570\u636e\u83b7\u53d6\u3001\u8ba2\u9605\u6216\u624b\u52a8\u4fee\u6539 DOM \u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useState, useEffect } from 'react';\n\nfunction App() {\n  const &#91;data, setData] = useState(&#91;]);\n\n  useEffect(() =&gt; {\n    fetch('https:\/\/api.example.com\/data')\n      .then(response =&gt; response.json())\n      .then(data =&gt; setData(data));\n  }, &#91;]); \/\/ \u7a7a\u6570\u7ec4\u8868\u793a\u53ea\u5728\u7ec4\u4ef6\u6302\u8f7d\u65f6\u6267\u884c\u4e00\u6b21\n\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Data:&lt;\/h1&gt;\n      &lt;ul&gt;\n        {data.map(item =&gt; &lt;li key={item.id}&gt;{item.name}&lt;\/li&gt;)}\n      &lt;\/ul&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useEffect<\/code> \u4f1a\u5728\u7ec4\u4ef6\u6e32\u67d3\u540e\u6267\u884c\u526f\u4f5c\u7528\u51fd\u6570\u3002\u7a7a\u6570\u7ec4 <code>[]<\/code> \u8868\u793a\u8be5\u526f\u4f5c\u7528\u53ea\u4f1a\u5728\u7ec4\u4ef6\u6302\u8f7d\u65f6\u6267\u884c\u4e00\u6b21\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">7.1 \u6e05\u7406\u526f\u4f5c\u7528<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u5728 <code>useEffect<\/code> \u4e2d\u8fd4\u56de\u4e00\u4e2a\u6e05\u7406\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u7ec4\u4ef6\u5378\u8f7d\u65f6\u6e05\u7406\u526f\u4f5c\u7528\uff08\u5982\u6e05\u7406\u5b9a\u65f6\u5668\u3001\u53d6\u6d88\u8ba2\u9605\u7b49\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>useEffect(() =&gt; {\n  const timer = setInterval(() =&gt; {\n    console.log('Timer is running');\n  }, 1000);\n\n  return () =&gt; clearInterval(timer); \/\/ \u6e05\u7406\u5b9a\u65f6\u5668\n}, &#91;]);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>React \u8def\u7531\uff08React Router\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">React Router \u662f React \u7684\u6807\u51c6\u8def\u7531\u5e93\uff0c\u7528\u4e8e\u5728\u4e0d\u540c\u9875\u9762\u4e4b\u95f4\u5bfc\u822a\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5 React Router\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install react-router-dom<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">8.1 \u914d\u7f6e\u8def\u7531<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>App.js<\/code> \u4e2d\u914d\u7f6e\u8def\u7531\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react';\nimport { BrowserRouter as Router, Route, Switch } from 'react-router-dom';\nimport Home from '.\/Home';\nimport About from '.\/About';\n\nfunction App() {\n  return (\n    &lt;Router&gt;\n      &lt;nav&gt;\n        &lt;a href=\"\/\"&gt;Home&lt;\/a&gt;\n        &lt;a href=\"\/about\"&gt;About&lt;\/a&gt;\n      &lt;\/nav&gt;\n      &lt;Switch&gt;\n        &lt;Route path=\"\/\" exact component={Home} \/&gt;\n        &lt;Route path=\"\/about\" component={About} \/&gt;\n      &lt;\/Switch&gt;\n    &lt;\/Router&gt;\n  );\n}\n\nexport default App;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>BrowserRouter<\/code>\uff1a\u5b9a\u4e49\u8def\u7531\u5bb9\u5668\u3002<\/li>\n\n\n\n<li><code>Route<\/code>\uff1a\u5b9a\u4e49\u8def\u7531\u548c\u7ec4\u4ef6\u7684\u6620\u5c04\u3002<\/li>\n\n\n\n<li><code>Switch<\/code>\uff1a\u53ea\u6e32\u67d3\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u8def\u7531\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>React \u6700\u65b0\u529f\u80fd\uff1aSuspense \u548c Concurrent Mode<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">React \u7684 Suspense \u548c\u5e76\u53d1\u6a21\u5f0f\uff08Concurrent Mode\uff09\u662f\u4e3a\u4e86\u6539\u5584\u7528\u6237\u4f53\u9a8c\u548c\u6027\u80fd\u800c\u8bbe\u8ba1\u7684\u3002Suspense \u5141\u8bb8\u4f60\u5728\u7ec4\u4ef6\u52a0\u8f7d\u65f6\u663e\u793a\u4e00\u4e2a\u52a0\u8f7d\u72b6\u6001\uff0c\u800c\u5e76\u53d1\u6a21\u5f0f\u5219\u901a\u8fc7\u66f4\u667a\u80fd\u7684\u8c03\u5ea6\u65b9\u5f0f\uff0c\u4f18\u5316\u5e94\u7528\u7684\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9.1 \u4f7f\u7528 Suspense<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { Suspense } from 'react';\n\nconst LazyComponent = React.lazy(() =&gt; import('.\/LazyComponent'));\n\nfunction App() {\n  return (\n    &lt;Suspense fallback={&lt;div&gt;Loading...&lt;\/div&gt;}&gt;\n\n\n &lt;LazyComponent \/&gt;\n    &lt;\/Suspense&gt;\n  );\n}\n\nexport default App;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>React.lazy<\/code> \u7528\u4e8e\u52a8\u6001\u52a0\u8f7d\u7ec4\u4ef6\uff0c<code>Suspense<\/code> \u7528\u4e8e\u5904\u7406\u7ec4\u4ef6\u52a0\u8f7d\u65f6\u7684 fallback \u72b6\u6001\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u5185\u5bb9\u6db5\u76d6\u4e86 React \u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ec\u7ec4\u4ef6\u3001\u72b6\u6001\u3001\u4e8b\u4ef6\u5904\u7406\u3001Hooks \u548c\u8def\u7531\u7b49\u3002React \u751f\u6001\u975e\u5e38\u4e30\u5bcc\uff0c\u968f\u7740\u7248\u672c\u7684\u66f4\u65b0\uff0c\u65b0\u7684\u529f\u80fd\u548c\u4f18\u5316\u4e0d\u65ad\u88ab\u6dfb\u52a0\u3002\u5e0c\u671b\u8fd9\u4e2a\u6559\u7a0b\u80fd\u5e2e\u52a9\u4f60\u638c\u63e1 React \u5e76\u5f00\u59cb\u6784\u5efa\u81ea\u5df1\u7684\u5e94\u7528\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React \u662f\u4e00\u4e2a\u7531 Facebook \u5f00\u53d1\u7684 JavaScript \u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u6784\u5efa\u7528\u6237\u754c\u9762\uff0c\u5c24\u5176\u9002\u5408\u6784\u5efa\u5355 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-649","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":650,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/649\/revisions\/650"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}