{"id":67,"date":"2024-01-15T19:48:13","date_gmt":"2024-01-15T11:48:13","guid":{"rendered":"http:\/\/hxlsakrua.top\/?p=67"},"modified":"2024-01-15T20:01:49","modified_gmt":"2024-01-15T12:01:49","slug":"react-hookahooks%e7%9a%84%e5%ad%a6%e4%b9%a0%e8%ae%b0%e5%bd%95%ef%bc%88%e6%8c%81%e7%bb%ad%e6%9b%b4%e6%96%b0%ef%bc%89","status":"publish","type":"post","link":"http:\/\/hxlsakrua.top\/?p=67","title":{"rendered":"\u200bReact-Hook+ahooks\u7684\u5b66\u4e60\u8bb0\u5f55\uff08\u6301\u7eed\u66f4\u65b0\uff09\u200b"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">&nbsp;\u7b80\u4ecb<\/h1>\n\n\n\n<p>hook\u94a9\u5b50\u53ef\u4ee5\u6781\u5927\u52a0\u5f3a\u5f00\u53d1\u6548\u7387\uff0c\u9002\u7528\u4e8ereact\u51fd\u6570\u5f0f\u7ec4\u4ef6\uff0c\u51fd\u6570\u5f0f\u7ec4\u4ef6\u6ca1\u6709\u751f\u547d\u5468\u671f\u4e8e\u662f\u7528\u94a9\u5b50\u51fd\u6570\u6765\u8fdb\u884c\u4ee3\u66ff\uff0c\u94a9\u5b50\u7684\u529f\u80fd\u5341\u5206\u5f3a\u5927\uff0c\u4f8b\u5982\u53ef\u4ee5\u5b9e\u73b0\u53cc\u5411\u6570\u636e\u7ed1\u5b9a\u7b49\u7b49\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u4e0b\u9762\u662f\u6211\u6574\u7406\u7684\u94a9\u5b50\u5b66\u4e60\u8bb0\u5f55\u3002\u53c2\u8003<a href=\"\" target=\"_blank\"  rel=\"nofollow\" >\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">1\u3001useCallback<\/h1>\n\n\n\n<p><code>useCallback<\/code> \u662f\u4e00\u4e2a\u5141\u8bb8\u4f60\u5728\u591a\u6b21\u6e32\u67d3\u4e2d\u7f13\u5b58\u51fd\u6570\u7684 React Hook\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const cachedFn = useCallback(fn, dependencies)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c2\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fn<\/code>\uff1a\u60f3\u8981\u7f13\u5b58\u7684\u51fd\u6570\u3002\u6b64\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u5e76\u4e14\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/li>\n\n\n\n<li><code>dependencies<\/code>\uff1a\u6709\u5173\u662f\u5426\u66f4\u65b0 <code>fn<\/code> \u7684\u6240\u6709\u54cd\u5e94\u5f0f\u503c\u7684\u4e00\u4e2a\u5217\u8868\u3002\u54cd\u5e94\u5f0f\u503c\u5305\u62ec props\u3001state\uff0c\u548c\u6240\u6709\u5728\u4f60\u7ec4\u4ef6\u5185\u90e8\u76f4\u63a5\u58f0\u660e\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<p>\u5728\u521d\u6b21\u6e32\u67d3\u65f6\uff0c<code>useCallback<\/code> \u8fd4\u56de\u4f60\u5df2\u7ecf\u4f20\u5165\u7684 <code>fn<\/code> \u51fd\u6570<\/p>\n\n\n\n<p>\u5728\u4e4b\u540e\u7684\u6e32\u67d3\u4e2d, \u5982\u679c\u4f9d\u8d56\u6ca1\u6709\u6539\u53d8\uff0c<code>useCallback<\/code> \u8fd4\u56de\u4e0a\u4e00\u6b21\u6e32\u67d3\u4e2d\u7f13\u5b58\u7684 <code>fn<\/code> \u51fd\u6570\uff1b\u5426\u5219\u8fd4\u56de\u8fd9\u4e00\u6b21\u6e32\u67d3\u4f20\u5165\u7684 <code>fn<\/code>\u3002<\/p>\n\n\n\n<p>\u7528\u6cd5<\/p>\n\n\n\n<p id=\"skipping-re-rendering-of-components\">\u8df3\u8fc7\u7ec4\u4ef6\u7684\u91cd\u65b0\u6e32\u67d3\uff0c\u5904\u7406\u6570\u636e\u83b7\u53d6\u4e0d\u53d8\u65f6\u7684\u91cd\u590d\u83b7\u53d6\u540e\u8fdb\u884c\u6e32\u67d3\uff0c\u5904\u7406\u51fa\u73b0\u95ea\u5c4f\u7b49\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u7684\u95ee\u9898<\/p>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useCallback, useState } from 'react';\n\n\nconst foo = () =&gt; {\n  const &#91;count, setCount] = useState(0);\n  const handleClick = useCallback(() =&gt; {\n    console.log('Clicked');\n  }, &#91;count]);\n\n  function Child({ handleClick }:any) {\n    console.log('Child component rendered');\n    return &lt;button onClick={handleClick}&gt;Click Me&lt;\/button&gt;;\n  }\n\n  return (\n    &lt;div&gt;\n      &lt;p&gt;Count: {count}&lt;\/p&gt;\n      &lt;button onClick={() =&gt; setCount(count + 1)}&gt;Increment&lt;\/button&gt;\n      &lt;Child handleClick={handleClick} \/&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default foo;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u7ed3\u679c<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"487\" width=\"734\" src=\"https:\/\/img-blog.csdnimg.cn\/0fcaafea7cb2407694f1cd50f87c060b.png\" alt=\"\"><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"505\" width=\"821\" src=\"https:\/\/img-blog.csdnimg.cn\/f977ebab021c4d78a0525674249e5511.png\" alt=\"\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" height=\"516\" width=\"621\" src=\"https:\/\/img-blog.csdnimg.cn\/fbca37f4552649929823b19f88eb49b2.png\" alt=\"\"><\/p>\n\n\n\n<p>\u6ce8\u91ca<\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>foo<\/code>\u662f\u4e00\u4e2a\u51fd\u6570\u7ec4\u4ef6\uff0c\u5b83\u6709\u4e00\u4e2a\u72b6\u6001<code>count<\/code>\u548c\u4e00\u4e2a\u5904\u7406\u70b9\u51fb\u4e8b\u4ef6\u7684\u51fd\u6570<code>handleClick<\/code>\u3002<code>handleClick<\/code>\u4e2d\u7684<code>console.log<\/code>\u8bed\u53e5\u53ea\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u8868\u793a\u5728\u771f\u5b9e\u573a\u666f\u4e2d\u53ef\u80fd\u6709\u66f4\u52a0\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><code>Child<\/code>\u662f<code>foo<\/code>\u7684\u5b50\u7ec4\u4ef6\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a<code>handleClick<\/code>\u51fd\u6570\u4f5c\u4e3aprops\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230\u4e00\u4e2a\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e0a\u3002<\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u70b9\u51fb<code>Increment<\/code>\u6309\u94ae\u65f6\uff0c<code>foo<\/code>\u7684\u72b6\u6001<code>count<\/code>\u4f1a\u9012\u589e\uff0c\u5bfc\u81f4\u5b83\u91cd\u65b0\u6e32\u67d3\u3002\u4f46\u662f\u7531\u4e8e<code>handleClick<\/code>\u662f\u901a\u8fc7<code>useCallback<\/code>\u7f13\u5b58\u7684\uff0c\u5b83\u7684\u51fd\u6570\u5b9e\u4f8b\u5728\u4e0d\u53d8\u7684\u60c5\u51b5\u4e0b\u88ab\u91cd\u590d\u4f7f\u7528\uff0c\u4e0d\u4f1a\u5bfc\u81f4<code>ChildComponent<\/code>\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u6ca1\u6709\u4f7f\u7528<code>useCallback<\/code>\uff0c\u800c\u662f\u76f4\u63a5\u5c06\u4e00\u4e2a\u5185\u8054\u51fd\u6570\u4f20\u9012\u7ed9<code>Child<\/code>\uff0c\u90a3\u4e48\u6bcf\u6b21<code>foo<\/code>\u91cd\u65b0\u6e32\u67d3\u65f6\uff0c\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5b9e\u4f8b\uff0c\u5bfc\u81f4<code>Child<\/code>\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528<code>useCallback<\/code>\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\uff0c\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">2\u3001useContext<\/h1>\n\n\n\n<p><code>useContext<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u53ef\u4ee5\u8ba9\u4f60\u8bfb\u53d6\u548c\u8ba2\u9605\u7ec4\u4ef6\u4e2d\u7684 context\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const value = useContext(SomeContext)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c2\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SomeContext<\/code>\uff1a\u5148\u524d\u7528&nbsp;<a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/createContext\" target=\"_blank\"  rel=\"nofollow\" >createContext<\/a>&nbsp;\u521b\u5efa\u7684 context\u3002context \u672c\u8eab\u4e0d\u5305\u542b\u4fe1\u606f\uff0c\u5b83\u53ea\u4ee3\u8868\u4f60\u53ef\u4ee5\u63d0\u4f9b\u6216\u4ece\u7ec4\u4ef6\u4e2d\u8bfb\u53d6\u7684\u4fe1\u606f\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<p><code>useContext<\/code> \u4e3a\u8c03\u7528\u7ec4\u4ef6\u8fd4\u56de context \u7684\u503c\u3002\u5b83\u88ab\u786e\u5b9a\u4e3a\u4f20\u9012\u7ed9\u6811\u4e2d\u8c03\u7528\u7ec4\u4ef6\u4e0a\u65b9\u6700\u8fd1\u7684<code>SomeContext.Provider<\/code> \u7684 <code>value<\/code>\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u6837\u7684 provider\uff0c\u90a3\u4e48\u8fd4\u56de\u503c\u5c06\u4f1a\u662f\u4e3a\u521b\u5efa\u8be5 context \u4f20\u9012\u7ed9 <a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/createContext\" target=\"_blank\"  rel=\"nofollow\" >createContext<\/a> \u7684 <code>defaultValue<\/code>\u3002\u8fd4\u56de\u7684\u503c\u59cb\u7ec8\u662f\u6700\u65b0\u7684\u3002\u5982\u679c context \u53d1\u751f\u53d8\u5316\uff0cReact \u4f1a\u81ea\u52a8\u91cd\u65b0\u6e32\u67d3\u8bfb\u53d6 context \u7684\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p>\u7528\u6cd5<\/p>\n\n\n\n<p id=\"passing-data-deeply-into-the-tree\">\u5411\u7ec4\u4ef6\u6811\u6df1\u5c42\u4f20\u9012\u6570\u636e\uff0c\u901a\u8fc7\u8ba2\u9605\u63a5\u6536\u65b9\u5f0f\uff0c\u7b80\u5316\u4e86\u591a\u4e2a\u7236\u5b50\u95f4\u4f20\u503c\u7684\u7e41\u7410\u6027<\/p>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { createContext,useContext } from \"react\";\n\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u4e0a\u4e0b\u6587\u5bf9\u8c61\nconst MyContext = createContext('');\n\n\/\/ \u5728\u4e0a\u4e0b\u6587\u5bf9\u8c61\u4e2d\u63d0\u4f9b\u9ed8\u8ba4\u503c\nconst MyProvider = ({ children }:any) =&gt; {\n  const value = \"Hello from Context!\";\n  \n  return (\n    &lt;MyContext.Provider value={value}&gt;\n      {children}\n    &lt;\/MyContext.Provider&gt;\n  );\n};\n\n\/\/ \u4f7f\u7528useContext\u8bbf\u95ee\u4e0a\u4e0b\u6587\u4e2d\u7684\u503c\nconst MyComponent = () =&gt; {\n  const contextValue = useContext(MyContext);\n  \n  return &lt;div&gt;{contextValue}&lt;\/div&gt;;\n};\n\n\/\/ \u5728\u6839\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u63d0\u4f9b\u8005\u7ec4\u4ef6\nconst App = () =&gt; {\n  return (\n    &lt;MyProvider&gt;\n      &lt;MyComponent \/&gt;\n    &lt;\/MyProvider&gt;\n  );\n};\nexport default App;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">3\u3001useDebugValue<\/h1>\n\n\n\n<p><code>useDebugValue<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5728 <a href=\"https:\/\/zh-hans.react.dev\/learn\/react-developer-tools\" target=\"_blank\"  rel=\"nofollow\" >React \u5f00\u53d1\u5de5\u5177<\/a> \u4e2d\u4e3a\u81ea\u5b9a\u4e49 Hook \u6dfb\u52a0\u6807\u7b7e\u3002\u7531\u4e8e\u4f5c\u8005\u4e0d\u5e38\u7528\uff0c\u56e0\u6b64\u4e0d\u5728\u8d58\u8ff0<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">4\u3001useDeferredValue<\/h1>\n\n\n\n<p><code>useDeferredValue<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5ef6\u8fdf\u66f4\u65b0 UI \u7684\u67d0\u4e9b\u90e8\u5206\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const deferredValue = useDeferredValue(value)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c2\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>value<\/code>\uff1a\u4f60\u60f3\u5ef6\u8fdf\u7684\u503c\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<p>\u5728\u7ec4\u4ef6\u7684\u521d\u59cb\u6e32\u67d3\u671f\u95f4\uff0c\u8fd4\u56de\u7684\u5ef6\u8fdf\u503c\u5c06\u4e0e\u4f60\u63d0\u4f9b\u7684\u503c\u76f8\u540c\u3002\u4f46\u662f\u5728\u7ec4\u4ef6\u66f4\u65b0\u65f6\uff0cReact \u5c06\u4f1a\u5148\u5c1d\u8bd5\u4f7f\u7528\u65e7\u503c\u8fdb\u884c\u91cd\u65b0\u6e32\u67d3\uff08\u56e0\u6b64\u5b83\u5c06\u8fd4\u56de\u65e7\u503c\uff09\uff0c\u7136\u540e\u518d\u5728\u540e\u53f0\u4f7f\u7528\u65b0\u503c\u8fdb\u884c\u53e6\u4e00\u4e2a\u91cd\u65b0\u6e32\u67d3\uff08\u8fd9\u65f6\u5b83\u5c06\u8fd4\u56de\u66f4\u65b0\u540e\u7684\u503c\uff09\u3002<\/p>\n\n\n\n<p>\u7528\u6cd5<\/p>\n\n\n\n<p id=\"showing-stale-content-while-fresh-content-is-loading\">\u5728\u65b0\u5185\u5bb9\u52a0\u8f7d\u671f\u95f4\u663e\u793a\u65e7\u5185\u5bb9\uff1b&nbsp;\u8868\u660e\u5185\u5bb9\u5df2\u8fc7\u65f6 \uff1b\u5ef6\u8fdf\u6e32\u67d3 UI \u7684\u67d0\u4e9b\u90e8\u5206 \u3002<\/p>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<p>\u7ed3\u679c<\/p>\n\n\n\n<p>\u5148\u8f93\u5165SADASD,\u518d\u9000\u4f4d\u5230SAD,Data\u4f4d\u7f6e\u7684\u6570\u636e\u51fa\u73b0\u5ef6\u8fdf\u5c55\u793a\u7684\u6548\u679c<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"235\" width=\"282\" src=\"https:\/\/img-blog.csdnimg.cn\/d56d426b5ed74acba69175ccde4d6efd.png\" alt=\"\"><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"158\" width=\"331\" src=\"https:\/\/img-blog.csdnimg.cn\/724ee58a96e1484290bfe6237bd37acc.png\" alt=\"\">\u200b<\/p>\n\n\n\n<p>\u6ce8\u91ca<\/p>\n\n\n\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>useState<\/code>\u6765\u7ba1\u7406\u8f93\u5165\u6846\u4e2d\u7684\u503c\u548c\u4ece\u540e\u53f0\u52a0\u8f7d\u7684\u6570\u636e\u3002\u5f53\u7528\u6237\u8f93\u5165\u65f6\uff0c\u6211\u4eec\u4f7f\u7528<code>useEffect<\/code>\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u6a21\u62df\u4ece\u540e\u53f0\u52a0\u8f7d\u6570\u636e\u3002\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u66f4\u65b0<code>data<\/code>\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u6570\u636e\u52a0\u8f7d\u5b8c\u6210\u4e4b\u524d\u4e0d\u6e32\u67d3<code>&lt;p&gt;<\/code>\u5143\u7d20\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u4f7f\u7528<code>useDeferredValue<\/code>\u5c06<code>data<\/code>\u7684\u5ef6\u8fdf\u7248\u672c\u8d4b\u7ed9<code>deferredData<\/code>\u3002\u8fd9\u610f\u5473\u7740<code>deferredData<\/code>\u5c06\u4f1a\u5728\u4e00\u6bb5\u65f6\u95f4\u540e\u66f4\u65b0\u4e3a\u6700\u65b0\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5c06<code>deferredData<\/code>\u6e32\u67d3\u5230\u5c4f\u5e55\u4e0a\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u6e32\u67d3<code>data<\/code>\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u6570\u636e\u52a0\u8f7d\u5b8c\u6210\u4e4b\u524d\uff0c\u7528\u6237\u4e5f\u4e0d\u4f1a\u770b\u5230\u65e7\u7684\u6216\u4e0d\u5b8c\u6574\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528<code>useDeferredValue<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u6e32\u67d3\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u786e\u4fdd\u6570\u636e\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u6e32\u67d3\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u672a\u52a0\u8f7d\u6216\u4e0d\u5b8c\u6574\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">5\u3001useEffect<\/h1>\n\n\n\n<p><code>useEffect<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u5b83\u5141\u8bb8\u4f60 <a href=\"https:\/\/zh-hans.react.dev\/learn\/synchronizing-with-effects\" target=\"_blank\"  rel=\"nofollow\" >\u5c06\u7ec4\u4ef6\u4e0e\u5916\u90e8\u7cfb\u7edf\u540c\u6b65<\/a>\u3002\u57fa\u672c\u4e0a\u662freact\u51fd\u6570\u5f0f\u7ec4\u4ef6\u5fc5\u7528hook\uff0c<a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useEffect#connecting-to-an-external-system\" target=\"_blank\"  rel=\"nofollow\" >\u5b98\u65b9\u6587\u6863<\/a>\u4e0a\u5f88\u6e05\u695a\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">6\u3001useId<\/h1>\n\n\n\n<p><code>useId<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u53ef\u4ee5\u751f\u6210\u4f20\u9012\u7ed9\u65e0\u969c\u788d\u5c5e\u6027\u7684\u552f\u4e00 ID\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const id = useId()<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<p><code>useId<\/code> \u8fd4\u56de\u4e00\u4e2a\u552f\u4e00\u7684\u5b57\u7b26\u4e32 ID\uff0c\u4e0e\u6b64\u7279\u5b9a\u7ec4\u4ef6\u4e2d\u7684 <code>useId<\/code> \u8c03\u7528\u76f8\u5173\u8054\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">7\u3001useImperativeHandle<\/h1>\n\n\n\n<p><code>useImperativeHandle<\/code> \u662f React \u4e2d\u7684\u4e00\u4e2a Hook\uff0c\u5b83\u80fd\u8ba9\u4f60\u81ea\u5b9a\u4e49\u7531 <a href=\"https:\/\/zh-hans.react.dev\/learn\/manipulating-the-dom-with-refs\" target=\"_blank\"  rel=\"nofollow\" >ref<\/a> \u66b4\u9732\u51fa\u6765\u7684\u53e5\u67c4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>useImperativeHandle(ref, createHandle, dependencies?)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c2\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ref<\/code>\uff1a\u8be5 <code>ref<\/code> \u662f\u4f60\u4ece <a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/forwardRef#render-function\" target=\"_blank\"  rel=\"nofollow\" >forwardRef \u6e32\u67d3\u51fd\u6570<\/a> \u4e2d\u83b7\u5f97\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/li>\n\n\n\n<li><code>createHandle<\/code>\uff1a\u8be5\u51fd\u6570\u65e0\u9700\u53c2\u6570\uff0c\u5b83\u8fd4\u56de\u4f60\u60f3\u8981\u66b4\u9732\u7684 ref \u7684\u53e5\u67c4\u3002\u8be5\u53e5\u67c4\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u7c7b\u578b\u3002\u901a\u5e38\uff0c\u4f60\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u4f60\u60f3\u66b4\u9732\u7684\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u53ef\u9009\u7684 <code>dependencies<\/code>\uff1a\u51fd\u6570 <code>createHandle<\/code> \u4ee3\u7801\u4e2d\u6240\u7528\u5230\u7684\u6240\u6709\u53cd\u5e94\u5f0f\u7684\u503c\u7684\u5217\u8868\u3002\u53cd\u5e94\u5f0f\u7684\u503c\u5305\u542b props\u3001\u72b6\u6001\u548c\u5176\u4ed6\u6240\u6709\u76f4\u63a5\u5728\u4f60\u7ec4\u4ef6\u4f53\u5185\u58f0\u660e\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002\u5018\u82e5\u4f60\u7684\u4ee3\u7801\u68c0\u67e5\u5668\u5df2 <a href=\"https:\/\/zh-hans.react.dev\/learn\/editor-setup#linting\" target=\"_blank\"  rel=\"nofollow\" >\u4e3a React \u914d\u7f6e\u597d<\/a>\uff0c\u5b83\u4f1a\u9a8c\u8bc1\u6bcf\u4e00\u4e2a\u53cd\u5e94\u5f0f\u7684\u503c\u662f\u5426\u88ab\u6b63\u786e\u6307\u5b9a\u4e3a\u4f9d\u8d56\u9879\u3002\u8be5\u5217\u8868\u7684\u957f\u5ea6\u5fc5\u987b\u662f\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u5e76\u4e14\u5fc5\u987b\u6309\u7167 <code>[dep1, dep2, dep3]<\/code> \u7684\u5f62\u5f0f\u7f57\u5217\u5404\u4f9d\u8d56\u9879\u3002React \u4f1a\u4f7f\u7528 <a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/is\" target=\"_blank\"  rel=\"nofollow\" >Object.is<\/a> \u6765\u6bd4\u8f83\u6bcf\u4e00\u4e2a\u4f9d\u8d56\u9879\u4e0e\u5176\u5bf9\u5e94\u7684\u4e4b\u524d\u503c\u3002\u5982\u679c\u4e00\u6b21\u91cd\u65b0\u6e32\u67d3\u5bfc\u81f4\u67d0\u4e9b\u4f9d\u8d56\u9879\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u6216\u4f60\u6ca1\u6709\u63d0\u4f9b\u8fd9\u4e2a\u53c2\u6570\u5217\u8868\uff0c\u4f60\u7684\u51fd\u6570 <code>createHandle<\/code> \u5c06\u4f1a\u88ab\u91cd\u65b0\u6267\u884c\uff0c\u800c\u65b0\u751f\u6210\u7684\u53e5\u67c4\u5219\u4f1a\u88ab\u5206\u914d\u7ed9 ref\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<p><code>useImperativeHandle<\/code> \u8fd4\u56de <code>undefined<\/code>\u3002<\/p>\n\n\n\n<p>\u7528\u6cd5<\/p>\n\n\n\n<p id=\"exposing-a-custom-ref-handle-to-the-parent-component\">\u5411\u7236\u7ec4\u4ef6\u66b4\u9732\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 ref \u53e5\u67c4 \uff1b\u66b4\u9732\u4f60\u81ea\u5df1\u7684\u547d\u4ee4\u5f0f\u65b9\u6cd5&nbsp;<\/p>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useRef, useImperativeHandle, forwardRef } from 'react';\n\n\/\/ \u5b50\u7ec4\u4ef6\nconst ChildComponent = forwardRef((props, ref) =&gt; {\n  const inputRef = useRef();\n\n  \/\/ \u66b4\u9732\u7ed9\u7236\u7ec4\u4ef6\u7684\u65b9\u6cd5\n  useImperativeHandle(ref, () =&gt; ({\n    focusInput: () =&gt; {\n      inputRef.current.focus();\n    },\n    getValue: () =&gt; {\n      return inputRef.current.value;\n    }\n  }));\n\n  return (\n    &lt;input type=\"text\" ref={inputRef} \/&gt;\n  );\n});\n\n\/\/ \u7236\u7ec4\u4ef6\nconst ParentComponent = () =&gt; {\n  const childRef = useRef();\n\n  const handleButtonClick = () =&gt; {\n    childRef.current.focusInput();\n    const value = childRef.current.getValue();\n    alert(value);\n  };\n\n  return (\n    &lt;div&gt;\n      &lt;ChildComponent ref={childRef} \/&gt;\n      &lt;button onClick={handleButtonClick}&gt;\u64cd\u4f5c\u5b50\u7ec4\u4ef6&lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default ParentComponent;\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u7ed3\u679c<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/f1bdc3a9d79d4c74bc45f1632ac65e6d.png\" alt=\"\" style=\"width: 1000px;\"><\/p>\n\n\n\n<p>&nbsp;\u6ce8\u91ca<\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b50\u7ec4\u4ef6<code>ChildComponent<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u8f93\u5165\u6846\u3002\u6211\u4eec\u4f7f\u7528<code>useRef<\/code>\u6765\u521b\u5efa\u4e00\u4e2a<code>inputRef<\/code>\u5f15\u7528\uff0c\u7528\u4e8e\u83b7\u53d6\u8f93\u5165\u6846\u7684DOM\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>useImperativeHandle<\/code>\u6765\u5b9a\u4e49\u5b50\u7ec4\u4ef6\u5411\u7236\u7ec4\u4ef6\u66b4\u9732\u7684\u63a5\u53e3\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u66b4\u9732\u4e86<code>focusInput<\/code>\u548c<code>getValue<\/code>\u4e24\u4e2a\u65b9\u6cd5\u3002<code>focusInput<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u7126\u70b9\u805a\u7126\u5230\u8f93\u5165\u6846\uff0c<code>getValue<\/code>\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728\u7236\u7ec4\u4ef6<code>ParentComponent<\/code>\u4e2d\u4f7f\u7528<code>useRef<\/code>\u6765\u521b\u5efa\u4e00\u4e2a<code>childRef<\/code>\u5f15\u7528\uff0c\u7528\u4e8e\u83b7\u53d6\u5b50\u7ec4\u4ef6\u7684\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u5728<code>handleButtonClick<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>childRef.current<\/code>\u6765\u8bbf\u95ee\u5b50\u7ec4\u4ef6\u7684\u65b9\u6cd5<code>focusInput<\/code>\u548c<code>getValue<\/code>\uff0c\u4ece\u800c\u76f4\u63a5\u64cd\u4f5c\u5b50\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u5f53\u6211\u4eec\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u5b50\u7ec4\u4ef6\u7684\u8f93\u5165\u6846\u4f1a\u805a\u7126\uff0c\u5e76\u4e14\u5f39\u51fa\u6846\u4f1a\u663e\u793a\u8f93\u5165\u6846\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f<code>useImperativeHandle<\/code>\u7684\u4f5c\u7528\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4f7f\u7528React Hooks\u65f6\uff0c\u901a\u8fc7\u5b9a\u4e49\u5b50\u7ec4\u4ef6\u5411\u7236\u7ec4\u4ef6\u66b4\u9732\u7684\u63a5\u53e3\uff0c\u5b9e\u73b0\u76f4\u63a5\u64cd\u4f5c\u5b50\u7ec4\u4ef6\u7684\u80fd\u529b\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">8\u3001useInsertionEffect<\/h1>\n\n\n\n<p><code>useInsertionEffect<\/code> \u662f\u4e3a CSS-in-JS \u5e93\u7684\u4f5c\u8005\u7279\u610f\u6253\u9020\u7684\u3002\u9664\u975e\u4f60\u6b63\u5728\u4f7f\u7528 CSS-in-JS \u5e93\u5e76\u4e14\u9700\u8981\u6ce8\u5165\u6837\u5f0f\uff0c\u5426\u5219\u4f60\u5e94\u8be5\u4f7f\u7528 <a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useEffect\" target=\"_blank\"  rel=\"nofollow\" >useEffect<\/a> \u6216\u8005 <a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useLayoutEffect\" target=\"_blank\"  rel=\"nofollow\" >useLayoutEffect<\/a>\u3002\u56e0\u6b64\u4e0d\u5728\u8d58\u8ff0\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">9\u3001useLayoutEffect<\/h1>\n\n\n\n<p><code>useLayoutEffect<\/code> \u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u5c3d\u53ef\u80fd\u4f7f\u7528 <a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useEffect\" target=\"_blank\"  rel=\"nofollow\" >useEffect<\/a>\u3002<code>useLayoutEffect<\/code> \u662f <a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useEffect\" target=\"_blank\"  rel=\"nofollow\" >useEffect<\/a> \u7684\u4e00\u4e2a\u7248\u672c\uff0c\u5728\u6d4f\u89c8\u5668\u91cd\u65b0\u7ed8\u5236\u5c4f\u5e55\u4e4b\u524d\u89e6\u53d1\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">10\u3001useMemo<\/h1>\n\n\n\n<p><code>useMemo<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u5b83\u5728\u6bcf\u6b21\u91cd\u65b0\u6e32\u67d3\u7684\u65f6\u5019\u80fd\u591f\u7f13\u5b58\u8ba1\u7b97\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const cachedValue = useMemo(calculateValue, dependencies)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c2\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>calculateValue<\/code>\uff1a\u8981\u7f13\u5b58\u8ba1\u7b97\u503c\u7684\u51fd\u6570\u3002\u5b83\u5e94\u8be5\u662f\u4e00\u4e2a\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\u7684\u7eaf\u51fd\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u8fd4\u56de\u4efb\u610f\u7c7b\u578b\u3002React \u5c06\u4f1a\u5728\u9996\u6b21\u6e32\u67d3\u65f6\u8c03\u7528\u8be5\u51fd\u6570\uff1b\u5728\u4e4b\u540e\u7684\u6e32\u67d3\u4e2d\uff0c\u5982\u679c <code>dependencies<\/code> \u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0cReact \u5c06\u76f4\u63a5\u8fd4\u56de\u76f8\u540c\u503c\u3002\u5426\u5219\uff0c\u5c06\u4f1a\u518d\u6b21\u8c03\u7528 <code>calculateValue<\/code> \u5e76\u8fd4\u56de\u6700\u65b0\u7ed3\u679c\uff0c\u7136\u540e\u7f13\u5b58\u8be5\u7ed3\u679c\u4ee5\u4fbf\u4e0b\u6b21\u91cd\u590d\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><code>dependencies<\/code>\uff1a\u6240\u6709\u5728 <code>calculateValue<\/code> \u51fd\u6570\u4e2d\u4f7f\u7528\u7684\u54cd\u5e94\u5f0f\u53d8\u91cf\u7ec4\u6210\u7684\u6570\u7ec4\u3002\u54cd\u5e94\u5f0f\u53d8\u91cf\u5305\u62ec props\u3001state \u548c\u6240\u6709\u4f60\u76f4\u63a5\u5728\u7ec4\u4ef6\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002\u5982\u679c\u4f60\u5728\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u4e2d <a href=\"https:\/\/zh-hans.react.dev\/learn\/editor-setup#linting\" target=\"_blank\"  rel=\"nofollow\" >\u914d\u7f6e\u4e86 React<\/a>\uff0c\u5b83\u5c06\u4f1a\u786e\u4fdd\u6bcf\u4e00\u4e2a\u54cd\u5e94\u5f0f\u6570\u636e\u90fd\u88ab\u6b63\u786e\u5730\u5b9a\u4e49\u4e3a\u4f9d\u8d56\u9879\u3002\u4f9d\u8d56\u9879\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u662f\u56fa\u5b9a\u7684\u5e76\u4e14\u5fc5\u987b\u5199\u6210 <code>[dep1, dep2, dep3]<\/code> \u8fd9\u79cd\u5f62\u5f0f\u3002React \u4f7f\u7528 <a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/is\" target=\"_blank\"  rel=\"nofollow\" >Object.is<\/a> \u5c06\u6bcf\u4e2a\u4f9d\u8d56\u9879\u4e0e\u5176\u4e4b\u524d\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<p>\u5728\u521d\u6b21\u6e32\u67d3\u65f6\uff0c<code>useMemo<\/code> \u8fd4\u56de\u4e0d\u5e26\u53c2\u6570\u8c03\u7528 <code>calculateValue<\/code> \u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u5728\u63a5\u4e0b\u6765\u7684\u6e32\u67d3\u4e2d\uff0c\u5982\u679c\u4f9d\u8d56\u9879\u6ca1\u6709\u53d1\u751f\u6539\u53d8\uff0c\u5b83\u5c06\u8fd4\u56de\u4e0a\u6b21\u7f13\u5b58\u7684\u503c\uff1b\u5426\u5219\u5c06\u518d\u6b21\u8c03\u7528 <code>calculateValue<\/code>\uff0c\u5e76\u8fd4\u56de\u6700\u65b0\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u7528\u6cd5<\/p>\n\n\n\n<p id=\"skipping-expensive-recalculations\">\u8df3\u8fc7\u4ee3\u4ef7\u6602\u8d35\u7684\u91cd\u65b0\u8ba1\u7b97\uff1b\u8df3\u8fc7\u7ec4\u4ef6\u7684\u91cd\u65b0\u6e32\u67d3\uff1b\u8bb0\u5fc6\u53e6\u4e00\u4e2a Hook \u7684\u4f9d\u8d56\uff1b\u8bb0\u5fc6\u4e00\u4e2a\u51fd\u6570&nbsp;<\/p>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useState, useMemo } from 'react';\n\nconst Example = () =&gt; {\n  const &#91;num1, setNum1] = useState(0);\n  const &#91;num2, setNum2] = useState(0);\n\n  const sum = useMemo(() =&gt; {\n    console.log('\u8ba1\u7b97\u548c');\n    return num1 + num2;\n  }, &#91;num1, num2]);\n\n  return (\n    &lt;div&gt;\n      &lt;div&gt;\n        &lt;label&gt;\u6570\u5b571\uff1a&lt;\/label&gt;\n        &lt;input\n          type=\"number\"\n          value={num1}\n          onChange={(e) =&gt; setNum1(parseInt(e.target.value))}\n        \/&gt;\n      &lt;\/div&gt;\n      &lt;div&gt;\n        &lt;label&gt;\u6570\u5b572\uff1a&lt;\/label&gt;\n        &lt;input\n          type=\"number\"\n          value={num2}\n          onChange={(e) =&gt; setNum2(parseInt(e.target.value))}\n        \/&gt;\n      &lt;\/div&gt;\n      &lt;p&gt;\u548c\uff1a{sum}&lt;\/p&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default Example;\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u7ed3\u679c<\/p>\n\n\n\n<p>\u5148\u5728\u6570\u5b571\u5904\u8f93\u516501\uff0c\u503c\u6539\u53d8\u5f00\u59cb\u8ba1\u7b97\uff0c\u518d\u5728\u6570\u5b572\u5904\u8f93\u516500\uff0c\u503c\u672a\u53d8\u5316\uff0c\u672a\u8ba1\u7b97\u8fd4\u56de\u539f\u6765\u7684\u503c<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"485\" width=\"396\" src=\"https:\/\/img-blog.csdnimg.cn\/ea4a2177d8fa4d8299a20010d8a873fe.png\" alt=\"\">\u200b<\/p>\n\n\n\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" height=\"496\" width=\"496\" src=\"https:\/\/img-blog.csdnimg.cn\/8007ded822c24359929b15df2c43690c.png\" alt=\"\">\u200b<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"492\" width=\"570\" src=\"https:\/\/img-blog.csdnimg.cn\/f3a535240cdc4e09981b6717db4bab55.png\" alt=\"\">\u200b<\/p>\n\n\n\n<p>&nbsp;\u6ce8\u91ca<\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528useState\u6765\u521b\u5efa\u4e24\u4e2a\u72b6\u6001\u53d8\u91cfnum1\u548cnum2\uff0c\u5206\u522b\u8868\u793a\u8f93\u5165\u7684\u4e24\u4e2a\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528useMemo\u6765\u7f13\u5b58\u8ba1\u7b97\u7684\u548c\u3002useMemo\u63a5\u53d7\u4e00\u4e2a\u4f9d\u8d56\u6570\u7ec4\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5f53\u4f9d\u8d56\u9879\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u624d\u4f1a\u91cd\u65b0\u8ba1\u7b97\u548c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06num1\u548cnum2\u4f5c\u4e3a\u4f9d\u8d56\u9879\uff0c\u53ea\u6709\u5f53\u5b83\u4eec\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u548c\u7684\u8ba1\u7b97\u624d\u4f1a\u89e6\u53d1\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u7ec4\u4ef6\u4e2d\u770b\u5230\u4e00\u4e2a\u663e\u793a\u548c\u7684p\u5143\u7d20\u3002\u5728\u6bcf\u6b21num1\u6216num2\u6539\u53d8\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\"\u8ba1\u7b97\u548c\"\u7684\u65e5\u5fd7\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u53ea\u6539\u53d8\u4e00\u4e2a\u6570\u5b57\uff0c\u53e6\u4e00\u4e2a\u6570\u5b57\u4fdd\u6301\u4e0d\u53d8\uff0c\u90a3\u4e48\u548c\u7684\u8ba1\u7b97\u5c06\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\uff0c\u5e76\u4e14\u4e0d\u4f1a\u91cd\u65b0\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u9a8c\u8bc1useMemo\u7684\u4f5c\u7528\uff0c\u5b83\u786e\u5b9e\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\uff0c\u5e76\u5728\u4f9d\u8d56\u9879\u6ca1\u6709\u6539\u53d8\u65f6\u91cd\u590d\u4f7f\u7528\u8fd9\u4e9b\u7ed3\u679c\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8React\u7ec4\u4ef6\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">11\u3001useReducer<\/h1>\n\n\n\n<p><code>useReducer<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u5b83\u5141\u8bb8\u4f60\u5411\u7ec4\u4ef6\u91cc\u9762\u6dfb\u52a0\u4e00\u4e2a <a href=\"https:\/\/zh-hans.react.dev\/learn\/extracting-state-logic-into-a-reducer\" target=\"_blank\"  rel=\"nofollow\" >reducer<\/a>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const &#91;state, dispatch] = useReducer(reducer, initialArg, init?)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>&nbsp;\u53c2\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>reducer<\/code>\uff1a\u7528\u4e8e\u66f4\u65b0 state \u7684\u7eaf\u51fd\u6570\u3002\u53c2\u6570\u4e3a state \u548c action\uff0c\u8fd4\u56de\u503c\u662f\u66f4\u65b0\u540e\u7684 state\u3002state \u4e0e action \u53ef\u4ee5\u662f\u4efb\u610f\u5408\u6cd5\u503c\u3002<\/li>\n\n\n\n<li><code>initialArg<\/code>\uff1a\u7528\u4e8e\u521d\u59cb\u5316 state \u7684\u4efb\u610f\u503c\u3002\u521d\u59cb\u503c\u7684\u8ba1\u7b97\u903b\u8f91\u53d6\u51b3\u4e8e\u63a5\u4e0b\u6765\u7684&nbsp;<code>init<\/code>&nbsp;\u53c2\u6570\u3002<\/li>\n\n\n\n<li>\u53ef\u9009\u53c2\u6570&nbsp;<code>init<\/code>\uff1a\u7528\u4e8e\u8ba1\u7b97\u521d\u59cb\u503c\u7684\u51fd\u6570\u3002\u5982\u679c\u5b58\u5728\uff0c\u4f7f\u7528&nbsp;<code>init(initialArg)<\/code>&nbsp;\u7684\u6267\u884c\u7ed3\u679c\u4f5c\u4e3a\u521d\u59cb\u503c\uff0c\u5426\u5219\u4f7f\u7528&nbsp;<code>initialArg<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>&nbsp;\u8fd4\u56de\u503c<\/p>\n\n\n\n<p><code>useReducer<\/code> \u8fd4\u56de\u4e00\u4e2a\u7531\u4e24\u4e2a\u503c\u7ec4\u6210\u7684\u6570\u7ec4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5f53\u524d\u7684 state\u3002\u521d\u6b21\u6e32\u67d3\u65f6\uff0c\u5b83\u662f&nbsp;<code>init(initialArg)<\/code>&nbsp;\u6216&nbsp;<code>initialArg<\/code>&nbsp;\uff08\u5982\u679c\u6ca1\u6709&nbsp;<code>init<\/code>&nbsp;\u51fd\u6570\uff09\u3002<\/li>\n\n\n\n<li><a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useReducer#dispatch\" target=\"_blank\"  rel=\"nofollow\" >dispatch&nbsp;\u51fd\u6570<\/a>\u3002\u7528\u4e8e\u66f4\u65b0 state \u5e76\u89e6\u53d1\u7ec4\u4ef6\u7684\u91cd\u65b0\u6e32\u67d3\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u7528\u6cd5<\/p>\n\n\n\n<p>\u7ba1\u7406\u5168\u5c40\u7684\u72b6\u6001\uff0c\u901a\u8fc7reducer\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u7ec4\u4ef6\u83b7\u53d6\u5230state\u91cc\u9762\u7684\u503c\uff0c\u65e0\u9700\u7236\u5b50\u4f20\u503c\u548c\u7ec4\u4ef6\u8ba2\u9605\uff0c\u6570\u636e\u5728\u4e0d\u540c\u7ec4\u4ef6\u4e2d\u4f20\u503c\u6b21\u6570\u8fc7\u591a\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528\uff0c\u51cf\u5c11\u4ee3\u7801\u7e41\u7410\u6027\uff0c\u65b9\u4fbf\u7ba1\u7406state\u503c\uff0c\u63d0\u5347\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u6211\u4eec\u7684\u7ec4\u4ef6\u4e2d\u5bfc\u5165useReducer\u51fd\u6570\uff1a\nimport React, { useReducer } from \"react\";\n\n\/\/\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2areducer\u51fd\u6570\uff0c\u5b83\u5c06\u6839\u636e\u6211\u4eec\u7684\u64cd\u4f5c\u7c7b\u578b\u6765\u66f4\u65b0\u6211\u4eec\u7684\u8ba1\u6570\u72b6\u6001\uff1a\nconst reducer = (state, action) =&gt; {\n  switch (action.type) {\n    case \"INCREMENT\":\n      return { count: state.count + 1 };\n    case \"DECREMENT\":\n      return { count: state.count - 1 };\n    default:\n      throw new Error();\n  }\n};\n\n\n\/\/\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6211\u4eec\u7684\u7ec4\u4ef6\u4e2d\u4f7f\u7528useReducer\u6765\u521d\u59cb\u5316\u6211\u4eec\u7684\u72b6\u6001\u548c\u52a8\u4f5c\u5206\u53d1\u51fd\u6570\u3002\nconst Counter = () =&gt; {\n  const initialState = { count: 0 };\n  const &#91;state, dispatch] = useReducer(reducer, initialState);\n  \n  \/\/ \u901a\u8fc7dispatch\u8c03\u7528\u4e0d\u540c\u7684\u64cd\u4f5c\u7c7b\u578b\u6765\u66f4\u65b0\u72b6\u6001\n  const increment = () =&gt; {\n    dispatch({ type: \"INCREMENT\" });\n  };\n\n  const decrement = () =&gt; {\n    dispatch({ type: \"DECREMENT\" });\n  };\n\n  return (\n    &lt;div&gt;\n      &lt;h2&gt;\u8ba1\u6570\u5668&lt;\/h2&gt;\n      &lt;p&gt;\u5f53\u524d\u8ba1\u6570\u503c: {state.count}&lt;\/p&gt;\n      &lt;button onClick={decrement}&gt;\u51cf\u5c11&lt;\/button&gt;\n      &lt;button onClick={increment}&gt;\u589e\u52a0&lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default Counter;\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u7ed3\u679c<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"156\" width=\"245\" src=\"https:\/\/img-blog.csdnimg.cn\/d480e74b9cbc4ffe944f386314b37cc5.png\" alt=\"\">\u200b<\/p>\n\n\n\n<p>&nbsp;\u6ce8\u91ca<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u8ba1\u6570\u5668\u5e94\u7528\u7a0b\u5e8f\uff0c\u5176\u4e2d\u6211\u4eec\u53ef\u4ee5\u589e\u52a0\u6216\u51cf\u5c11\u8ba1\u6570\u503c\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528dispatch\u51fd\u6570\u6765\u5206\u53d1\u4e0d\u540c\u7684\u64cd\u4f5c\u7c7b\u578b\uff0c\u4ece\u800c\u66f4\u65b0\u6211\u4eec\u7684\u72b6\u6001\u3002\u4f7f\u7528useReducer\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5730\u65b9\u96c6\u4e2d\u5904\u7406\u6240\u6709\u64cd\u4f5c\uff0c\u5e76\u6839\u636e\u5f53\u524d\u72b6\u6001\u8fd4\u56de\u65b0\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">12\u3001useRef<\/h1>\n\n\n\n<p><code>useRef<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u5b83\u80fd\u8ba9\u4f60\u5f15\u7528\u4e00\u4e2a\u4e0d\u9700\u8981\u6e32\u67d3\u7684\u503c\u3002\u5e38\u7528\u4e8eref\u5168\u5c40\u5f15\u7528\u4e00\u4e2a\u503c\uff0c\u4fdd\u8bc1\u7ec4\u4ef6\u66f4\u65b0\u4e0d\u4f1a\u6e32\u67d3\uff1b\u901a\u8fc7ref\u64cd\u4f5cdom\uff0c\u907f\u514d\u91cd\u590d\u521b\u5efaref\u5185\u5bb9\uff0c\u53c2\u8003<a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useRef\" target=\"_blank\"  rel=\"nofollow\" >\u5b98\u65b9\u6587\u6863<\/a>\uff0c\u4e0d\u5728\u8d58\u8ff0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const ref = useRef(initialValue)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">13\u3001useState<\/h1>\n\n\n\n<p><code>useState<\/code> \u662f\u4e00\u4e2a React Hook\uff0c\u5b83\u5141\u8bb8\u4f60\u5411\u7ec4\u4ef6\u6dfb\u52a0\u4e00\u4e2a <a href=\"https:\/\/zh-hans.react.dev\/learn\/state-a-components-memory\" target=\"_blank\"  rel=\"nofollow\" >\u72b6\u6001\u53d8\u91cf<\/a>\u3002\u53c2\u8003<a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useState\" target=\"_blank\"  rel=\"nofollow\" >\u5b98\u65b9\u6587\u6863<\/a>\uff0c\u4e0d\u5728\u8d58\u8ff0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const &#91;state, setState] = useState(initialState);<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">14\u3001useSyncExternalStore<\/h1>\n\n\n\n<p><code>useSyncExternalStore<\/code> \u662f\u4e00\u4e2a\u8ba9\u4f60\u8ba2\u9605\u5916\u90e8 store \u7684 React Hook\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const snapshot = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot?)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c2\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>subscribe<\/code>\uff1a\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u6536\u4e00\u4e2a\u5355\u72ec\u7684 <code>callback<\/code> \u53c2\u6570\u5e76\u628a\u5b83\u8ba2\u9605\u5230 store \u4e0a\u3002\u5f53 store \u53d1\u751f\u6539\u53d8\uff0c\u5b83\u5e94\u5f53\u8c03\u7528\u88ab\u63d0\u4f9b\u7684 <code>callback<\/code>\u3002\u8fd9\u4f1a\u5bfc\u81f4\u7ec4\u4ef6\u91cd\u65b0\u6e32\u67d3\u3002<code>subscribe<\/code> \u51fd\u6570\u4f1a\u8fd4\u56de\u6e05\u9664\u8ba2\u9605\u7684\u51fd\u6570\u3002<\/li>\n\n\n\n<li><code>getSnapshot<\/code>\uff1a\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd4\u56de\u7ec4\u4ef6\u9700\u8981\u7684 store \u4e2d\u7684\u6570\u636e\u5feb\u7167\u3002\u5728 store \u4e0d\u53d8\u7684\u60c5\u51b5\u4e0b\uff0c\u91cd\u590d\u8c03\u7528 <code>getSnapshot<\/code> \u5fc5\u987b\u8fd4\u56de\u540c\u4e00\u4e2a\u503c\u3002\u5982\u679c store \u6539\u53d8\uff0c\u5e76\u4e14\u8fd4\u56de\u503c\u4e5f\u4e0d\u540c\u4e86\uff08\u7528 <a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/is\" target=\"_blank\"  rel=\"nofollow\" >Object.is<\/a> \u6bd4\u8f83\uff09\uff0cReact \u5c31\u4f1a\u91cd\u65b0\u6e32\u67d3\u7ec4\u4ef6\u3002<\/li>\n\n\n\n<li>\u53ef\u9009 <code>getServerSnapshot<\/code>\uff1a\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd4\u56de store \u4e2d\u6570\u636e\u7684\u521d\u59cb\u5feb\u7167\u3002\u5b83\u53ea\u4f1a\u5728\u670d\u52a1\u7aef\u6e32\u67d3\u65f6\uff0c\u4ee5\u53ca\u5728\u5ba2\u6237\u7aef\u8fdb\u884c\u670d\u52a1\u7aef\u6e32\u67d3\u5185\u5bb9\u7684 hydration \u65f6\u88ab\u7528\u5230\u3002\u5feb\u7167\u5728\u670d\u52a1\u7aef\u4e0e\u5ba2\u6237\u7aef\u4e4b\u95f4\u5fc5\u987b\u76f8\u540c\uff0c\u5b83\u901a\u5e38\u662f\u4ece\u670d\u52a1\u7aef\u5e8f\u5217\u5316\u5e76\u4f20\u5230\u5ba2\u6237\u7aef\u7684\u3002\u5982\u679c\u4f60\u5ffd\u7565\u6b64\u53c2\u6570\uff0c\u5728\u670d\u52a1\u7aef\u6e32\u67d3\u8fd9\u4e2a\u7ec4\u4ef6\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<p>\u8be5 store \u7684\u5f53\u524d\u5feb\u7167\uff0c\u53ef\u4ee5\u5728\u4f60\u7684\u6e32\u67d3\u903b\u8f91\u4e2d\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u7528\u6cd5<\/p>\n\n\n\n<p id=\"subscribing-to-an-external-store\">\u8ba2\u9605\u5916\u90e8 store\uff1b\u8ba2\u9605\u6d4f\u89c8\u5668 API \uff1b\u628a\u903b\u8f91\u62bd\u53d6\u5230\u81ea\u5b9a\u4e49 Hook\uff1b\u6dfb\u52a0\u670d\u52a1\u7aef\u6e32\u67d3\u652f\u6301<\/p>\n\n\n\n<p>\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u8fd9\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9 store \u7684\u4f8b\u5b50\uff0c\n\/\/ \u4f60\u53ef\u80fd\u9700\u8981\u628a\u5b83\u4e0e React \u96c6\u6210\u3002\n\n\/\/ \u5982\u679c\u4f60\u7684\u5e94\u7528\u5b8c\u5168\u7531 React \u6784\u5efa\uff0c\n\/\/ \u6211\u4eec\u63a8\u8350\u4f7f\u7528 React state \u66ff\u4ee3\u3002\n\nlet nextId = 0;\nlet todos = &#91;{ id: nextId++, text: \"Todo #1\" }];\nlet listeners: any = &#91;];\n\nexport const todosStore = {\n  addTodo() {\n    todos = &#91;...todos, { id: nextId++, text: \"Todo #\" + nextId }];\n    emitChange();\n  },\n  subscribe(listener: any) {\n    listeners = &#91;...listeners, listener];\n    return () =&gt; {\n      listeners = listeners.filter((l: any) =&gt; l !== listener);\n    };\n  },\n  getSnapshot() {\n    return todos;\n  },\n};\n\nfunction emitChange() {\n  for (let listener of listeners) {\n    listener();\n  }\n}\n\nimport { useSyncExternalStore } from \"react\";\n\nfunction TodosApp() {\n  const todos = useSyncExternalStore(\n    todosStore.subscribe,\n    todosStore.getSnapshot\n  );\n  return (\n    &lt;&gt;\n      &lt;button onClick={() =&gt; todosStore.addTodo()}&gt;Add todo&lt;\/button&gt;\n      &lt;hr \/&gt;\n      &lt;ul&gt;\n        {todos.map((todo) =&gt; (\n          &lt;li key={todo.id}&gt;{todo.text}&lt;\/li&gt;\n        ))}\n      &lt;\/ul&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default TodosApp;\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u7ed3\u679c<\/p>\n\n\n\n<p>\u70b9\u51fbadd\u589e\u52a0\u4e00\u6761<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"132\" width=\"560\" src=\"https:\/\/img-blog.csdnimg.cn\/4fee0c421d154c899ea7cd2dc6020dfa.png\" alt=\"\">\u200b<\/p>\n\n\n\n<p>\u6ce8\u91ca<\/p>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528\u7b2c\u4e09\u65b9store\u7684\u4f8b\u5b50\u3002\u8fd9\u4e2astore\u7528\u6765\u7ba1\u7406todo\u5217\u8868\u3002\u5b83\u6709\u4e00\u4e2aaddTodo\u65b9\u6cd5\u7528\u6765\u6dfb\u52a0\u65b0\u7684todo\uff0c\u4e00\u4e2asubscribe\u65b9\u6cd5\u7528\u6765\u8ba2\u9605store\u7684\u53d8\u5316\uff0c\u4e00\u4e2agetSnapshot\u65b9\u6cd5\u7528\u6765\u83b7\u53d6\u5f53\u524d\u7684todo\u5217\u8868\u3002\u5728store\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u4f1a\u8c03\u7528\u8ba2\u9605\u7684\u76d1\u542c\u5668\u51fd\u6570\u6765\u66f4\u65b0UI\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2anextId\u53d8\u91cf\u6765\u8ddf\u8e2a\u4e0b\u4e00\u4e2atodo\u7684id\uff0c\u4ee5\u53ca\u4e00\u4e2atodos\u6570\u7ec4\u6765\u5b58\u50a8\u6240\u6709\u7684todos\u3002<\/li>\n\n\n\n<li>addTodo\u65b9\u6cd5\u4f1a\u5728todos\u6570\u7ec4\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684todo\u5bf9\u8c61\uff0c\u5e76\u81ea\u589enextId\u3002\u7136\u540e\u8c03\u7528emitChange\u51fd\u6570\u6765\u901a\u77e5\u6240\u6709\u7684\u76d1\u542c\u5668\u3002<\/li>\n\n\n\n<li>subscribe\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u76d1\u542c\u5668\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230listeners\u6570\u7ec4\u4e2d\u3002\u8fd4\u56de\u4e00\u4e2a\u53d6\u6d88\u8ba2\u9605\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u4e0d\u9700\u8981\u76d1\u542c\u7684\u65f6\u5019\u53d6\u6d88\u8ba2\u9605\u3002<\/li>\n\n\n\n<li>getSnapshot\u65b9\u6cd5\u8fd4\u56de\u5f53\u524d\u7684todos\u6570\u7ec4\u3002<\/li>\n\n\n\n<li>emitChange\u51fd\u6570\u4f1a\u904d\u5386\u6240\u6709\u7684\u76d1\u542c\u5668\u51fd\u6570\uff0c\u5e76\u4f9d\u6b21\u8c03\u7528\u5b83\u4eec\u3002<\/li>\n\n\n\n<li>\u5728\u4f7f\u7528\u8be5store\u7684\u7ec4\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528useSyncExternalStore hook\u6765\u8ba2\u9605store\u7684\u53d8\u5316\u5e76\u540c\u6b65\u6570\u636e\u5230\u7ec4\u4ef6\u4e2d\u3002<\/li>\n\n\n\n<li>\u5728TodosApp\u7ec4\u4ef6\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528useSyncExternalStore\u5e76\u4f20\u5165subscribe\u548cgetSnapshot\u65b9\u6cd5\u6765\u8ba2\u9605store\u7684\u53d8\u5316\u5e76\u83b7\u53d6\u5f53\u524d\u7684todos\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u5728render\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u6e32\u67d3\u4e00\u4e2a\u6309\u94ae\u6765\u6dfb\u52a0\u65b0\u7684todo\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u4f1a\u8c03\u7528todosStore.addTodo\u65b9\u6cd5\u6765\u6dfb\u52a0\u65b0\u7684todo\u3002<\/li>\n\n\n\n<li>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06todos\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2atodo\u6e32\u67d3\u6210\u4e00\u4e2a\u5217\u8868\u9879\u3002<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">&nbsp;15\u3001useTransition<\/h1>\n\n\n\n<p><code>useTransition<\/code> \u662f\u4e00\u4e2a\u8ba9\u4f60\u5728\u4e0d\u963b\u585e UI \u7684\u60c5\u51b5\u4e0b\u6765\u66f4\u65b0\u72b6\u6001\u7684 React Hook\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const &#91;isPending, startTransition] = useTransition()<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<p><code>useTransition<\/code> \u8fd4\u56de\u4e00\u4e2a\u7531\u4e24\u4e2a\u5143\u7d20\u7ec4\u6210\u7684\u6570\u7ec4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>isPending<\/code>&nbsp;\u6807\u5fd7\uff0c\u544a\u8bc9\u4f60\u662f\u5426\u5b58\u5728\u5f85\u5904\u7406\u7684\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useTransition#starttransition\" target=\"_blank\"  rel=\"nofollow\" >startTransition&nbsp;\u51fd\u6570<\/a>&nbsp;\u5141\u8bb8\u4f60\u5c06\u72b6\u6001\u66f4\u65b0\u6807\u8bb0\u4e3a\u8f6c\u6362\u72b6\u6001\u3002<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">ahooks\uff08\u5c01\u88c5\u4e86\u8bb8\u591a\u94a9\u5b50\uff0c\u63d0\u5347\u5f00\u53d1\u6548\u7387\uff0c\u63a8\u8350\u4f7f\u7528\uff09<\/h1>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> npm install --save ahooks<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/ahooks.js.org\/zh-CN\/hooks\/use-request\/index\" target=\"_blank\"  rel=\"nofollow\" >\u5b98\u65b9\u6587\u6863<\/a> \u200b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;\u7b80\u4ecb hook\u94a9\u5b50\u53ef\u4ee5\u6781\u5927\u52a0\u5f3a\u5f00\u53d1\u6548\u7387\uff0c\u9002\u7528\u4e8ereact\u51fd\u6570\u5f0f\u7ec4\u4ef6\uff0c\u51fd\u6570\u5f0f\u7ec4\u4ef6\u6ca1\u6709\u751f\u547d\u5468\u671f\u4e8e\u662f\u7528\u94a9\u5b50\u51fd\u6570\u6765\u8fdb\u884c\u4ee3\u66ff\uff0c\u94a9 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[9],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-4","tag-react"],"_links":{"self":[{"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=67"}],"version-history":[{"count":2,"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":70,"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions\/70"}],"wp:attachment":[{"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hxlsakrua.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}