{"id":53200,"date":"2024-12-03T17:45:13","date_gmt":"2024-12-03T09:45:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53200\/"},"modified":"2024-12-03T17:45:13","modified_gmt":"2024-12-03T09:45:13","slug":"%e6%88%91%e5%af%b9-use-%e9%92%a9%e5%ad%90%e7%9a%84%e6%80%9d%e8%80%83-%e6%b7%b1%e5%85%a5%e6%8e%a2%e8%ae%a8-react-%e7%9a%84%e6%9c%80%e6%96%b0%e5%ae%9e%e9%aa%8c%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53200\/","title":{"rendered":"\u6211\u5bf9 use() \u94a9\u5b50\u7684\u601d\u8003\u2014\u2014\u6df1\u5165\u63a2\u8ba8 React \u7684\u6700\u65b0\u5b9e\u9a8c\u529f\u80fd"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u6211\u5bf9 use() \u94a9\u5b50\u7684\u601d\u8003\u2014\u2014\u6df1\u5165\u63a2\u8ba8 React \u7684\u6700\u65b0\u5b9e\u9a8c\u529f\u80fd<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6211\u5bf9 use() \u94a9\u5b50\u7684\u601d\u8003\u2014\u2014\u6df1\u5165\u63a2\u8ba8 React \u7684\u6700\u65b0\u5b9e\u9a8c\u529f\u80fd\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/17327543906747bbd6c1c3e.jpg\" class=\"aligncenter\" title=\"\u6211\u5bf9 use() \u94a9\u5b50\u7684\u601d\u8003\u2014\u2014\u6df1\u5165\u63a2\u8ba8 React \u7684\u6700\u65b0\u5b9e\u9a8c\u529f\u80fd\u63d2\u56fe\" alt=\"\u6211\u5bf9 use() \u94a9\u5b50\u7684\u601d\u8003\u2014\u2014\u6df1\u5165\u63a2\u8ba8 React \u7684\u6700\u65b0\u5b9e\u9a8c\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>react 19 \u5df2\u7ecf\u53d1\u5e03\u4e86\uff0c\u5b83\u5e26\u6765\u4e86\u8bb8\u591a\u65b0\u529f\u80fd\uff0c\u4f8b\u5982\u670d\u52a1\u5668\u7ec4\u4ef6\u3001\u6307\u4ee4\uff08\u4f7f\u7528\u5ba2\u6237\u7aef\u548c\u4f7f\u7528\u670d\u52a1\u5668\uff09\u3001\u65b0\u94a9\u5b50\uff08\u4f8b\u5982 useoptimistic()\u3001useformstatus() \u548c\u5b9e\u9a8c\u6027 use()\uff09 hook\uff0c\u8fd9\u5c31\u662f\u6211\u4eca\u5929\u8981\u8bb2\u7684\u5185\u5bb9\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f use() \u6302\u94a9\uff1f <\/h2>\n<p>use() \u94a9\u5b50\u662f\u4e00\u9879\u65b0\u529f\u80fd\uff0c\u53ef\u8ba9\u60a8\u76f4\u63a5\u5728\u7ec4\u4ef6\u4e2d\u5904\u7406 promise\u3002\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u5728\u7ec4\u4ef6\u5185\u90e8\u89e3\u5f00 promise \u5e76\u66f4\u7b80\u6d01\u5730\u5904\u7406\u5f02\u6b65\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/p>\n<pre>import { use } from 'react';\n\/\/ example async function\nasync function fetchuserdetails(id) {\nconst response = await fetch(`localhost:3000\/api\/users\/${id}`);\nreturn response.json();\n}\nfunction userprofile({ id }) {\n\/\/ use() will suspend the component while the promise resolves\nconst user = use(fetchuser(id));\nreturn &lt;div&gt;hello, {user.name}!&lt;\/div&gt;;\n}\n<\/pre>\n<p>use() \u94a9\u5b50\u4ee3\u8868\u4e86 react \u5904\u7406\u5f02\u6b65\u6570\u636e\u65b9\u5f0f\u7684\u91cd\u5927\u8f6c\u53d8\uff0c\u4f7f\u5176\u66f4\u52a0\u76f4\u89c2\u5e76\u964d\u4f4e\u4e86\u7ba1\u7406\u5f02\u6b65\u72b6\u6001\u7684\u590d\u6742\u6027\u3002<\/p>\n<h2> use() \u94a9\u5b50\u7684\u4e3b\u8981\u7279\u70b9\uff1a <\/h2>\n<ul>\n<li>\n<p><strong>promise \u5904\u7406<\/strong>\uff1ause() \u53ef\u4ee5\u5904\u7406\u7ec4\u4ef6\u4e2d\u4efb\u4f55\u7ea7\u522b\u7684 promise\u3002\u5b83\u4f1a\u5728\u7b49\u5f85 promise \u89e3\u51b3\u65f6\u81ea\u52a8\u6302\u8d77\u7ec4\u4ef6\uff0c\u5e76\u4e14\u4e0e react \u7684 suspense \u8fb9\u754c\u914d\u5408\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406\u66f4\u76f4\u89c2<\/strong>:<\/p>\n<\/li>\n<\/ul>\n<pre>try {\nconst data = use(riskyoperation());\nreturn &lt;success data={data} \/&gt;;\n} catch (error) {\nreturn &lt;errorboundary error={error} \/&gt;;\n}\n<\/pre>\n<ul>\n<li> <strong>\u8d44\u6e90\u7f13\u5b58<\/strong>\uff1areact \u81ea\u52a8\u7f13\u5b58 use() \u7684\u7ed3\u679c \u2014 \u4e0d\u4f1a\u4e0d\u5fc5\u8981\u5730\u91cd\u65b0\u83b7\u53d6\u76f8\u540c\u7684 promise\uff0c\u4ece\u800c\u4ee5\u66f4\u5c11\u7684\u4ee3\u7801\u884c\u4f18\u5316\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2> <strong>\u6bd4\u8f83 use() \u4e0e usestate() useeffect() \u6a21\u5f0f<\/strong> <\/h2>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a api \u83b7\u53d6\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u5e16\u5b50\uff0c\u6211\u4eec\u9700\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5168\u5c40\u8bbf\u95ee\u5e16\u5b50\u3002<\/p>\n<pre>\/\/ global api fetch function\nasync function fetchuserposts(userid: string) {\nconst response = await fetch(`\/api\/users\/${userid}\/posts`);\nreturn response.json();\n}\n<\/pre>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u7528\u6237\u914d\u7f6e\u6587\u4ef6\u7ec4\u4ef6\u4e2d\u83b7\u53d6\u5e16\u5b50\uff0c\u5e76\u4f7f\u7528 usestate \u6302\u94a9\u548c useeffect \u6302\u94a9\u5c06\u5176\u6570\u636e\u4f5c\u4e3a\u5e16\u5b50\u72b6\u6001\u4f20\u9012\uff0c\u540c\u65f6\u5fc5\u987b\u5177\u6709\u6211\u4eec\u4e60\u60ef\u7684\u52a0\u8f7d\u72b6\u6001\u548c\u9519\u8bef\u72b6\u6001\u3002 <\/p>\n<pre>\/\/ example 1: basic data fetching\n\/\/ traditional approach using usestate and useeffect\nfunction userprofilepost({ postid }: { postid: string }) {\nconst [post, setpost] = usestate&lt;any&gt;(null);\nconst [isloading, setisloading] = usestate(true);\nconst [error, seterror] = usestate&lt;error | null&gt;(null);\n\nuseeffect(() =&gt; {\nsetisloading(true);\nseterror(null);\nfetchuserposts(userid)\n.then(data =&gt; {\nsetpost(data);\n})\n.catch(err =&gt; {\nseterror(err);\n})\n.finally(() =&gt; {\nsetisloading(false);\n});\n}, [userid]);\n\nif (isloading) return &lt;div&gt;&lt;loading \/&gt;&lt;\/div&gt;;\nif (error) return &lt;div&gt;&lt;error \/&gt;&lt;\/div&gt;;\nif (!post) return null;\n\nreturn (\n&lt;div&gt;\n&lt;h1&gt;{post.title}&lt;\/h1&gt;\n&lt;p&gt;{post.author}&lt;\/p&gt;\n&lt;\/div&gt;\n);\n}\n<\/pre>\n<p>\u4ee5\u4e0b\u662f\u6211\u4eec\u5982\u4f55\u4f7f\u7528 use() \u94a9\u5b50\u4ee5\u66f4\u5c11\u7684\u4ee3\u7801\u884c\u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u6d88\u9664\u4e86\u4f7f\u7528 usestate \u548c useeffect \u94a9\u5b50\u6765\u83b7\u53d6\u6570\u636e\u3001\u52a0\u8f7d\u72b6\u6001\u548c\u9519\u8bef\u72b6\u6001\u7684\u9700\u8981\uff0c\u540c\u65f6\u4ecd\u7136\u5b9e\u73b0\u8d44\u6e90\u7f13\u5b58\u4ee5\u6539\u8fdb\u8868\u6f14\u3002<\/p>\n<pre>\/\/ modern approach with use()\nfunction userprofilepost{ postid }: { postid: string }) {\nconst post= use(fetchuserpost(postid));\nreturn (\n&lt;suspense fallback=&lt;loading \/&gt;&gt;\n&lt;div&gt;\n&lt;errorboundary fallback=&lt;error \/&gt;&gt;\n&lt;h1&gt;{post.title}&lt;\/h1&gt;\n&lt;p&gt;{post.author}&lt;\/p&gt;\n&lt;\/errorboundary&gt;\n&lt;\/div&gt;\n&lt;\/suspense&gt;\n);\n}\n<\/pre>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u770b\u53e6\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u793a\u4f8b\u3002<\/p>\n<pre>\/\/ form submission with loading states\n\/\/ traditional approach using usestate and useeffect\nfunction submitformtraditional() {\nconst [issubmitting, setissubmitting] = usestate(false);\nconst [error, seterror] = usestate&lt;error | null&gt;(null);\nconst [success, setsuccess] = usestate(false);\n\nasync function handlesubmit(formdata: formdata) {\nsetissubmitting(true);\nseterror(null);\nsetsuccess(false);\n\ntry {\nawait fetch('localhost:3000\/api\/submit', {\nmethod: 'post',\nbody: formdata\n});\nsetsuccess(true);\n} catch (err: any) {\nseterror(err);\n} finally {\nsetissubmitting(false);\n}\n}\n\nreturn (\n&lt;form onsubmit={e =&gt; {\ne.preventdefault();\nhandlesubmit(new formdata(e.currenttarget));\n}}&gt;\n{\/* form fields *\/}\n&lt;button disabled={issubmitting}&gt;\n{issubmitting ? 'submitting\u2026' : 'submit'}\n&lt;\/button&gt;\n{error &amp;&amp; &lt;div&gt;&lt;error \/&gt;&lt;\/div&gt;}\n{success &amp;&amp; &lt;div&gt;&lt;success \/&gt;&lt;\/div&gt;}\n&lt;\/form&gt;\n);\n}\n<\/pre>\n<p>\u4e0b\u9762\u662f\u6211\u4eec\u5982\u4f55\u4f7f\u7528 use() \u94a9\u5b50\u505a\u540c\u6837\u7684\u4e8b\u60c5\u3002<\/p>\n<pre>\/\/ modern approach with use()\nasync function submitform(formdata: formdata) {\nconst response = await fetch('localhost:3000\/api\/submit', {\nmethod: 'post',\nbody: formdata\n});\nreturn response.json();\n}\n\nfunction submitform() {\nconst [formstate, setformstate] = usestate&lt;promise&lt;any&gt; | null&gt;(null);\nreturn (\n&lt;form onsubmit={e =&gt; {\ne.preventdefault();\nsetformstate(submitform(new formdata(e.currenttarget)));\n}}&gt;\n{\/* form fields *\/}\n&lt;button&gt;submit&lt;\/button&gt;\n{formstate &amp;&amp; (\n&lt;suspense fallback={&lt;div&gt;submitting\u2026&lt;\/div&gt;}&gt;\n&lt;formresult promise={formstate} \/&gt;\n&lt;\/suspense&gt;\n)}\n&lt;\/form&gt;\n);\n}\n\nfunction formresult({ promise }: { promise: promise&lt;any&gt; }) {\nconst result = use(promise);\nreturn &lt;div&gt;submitted successfully: {result.id}&lt;\/div&gt;;\n}\n<\/pre>\n<h2> use() \u94a9\u5b50\u65b9\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u548c\u4f18\u70b9\uff1a <\/h2>\n<h3> 1. \u7b80\u5316\u7684\u4ee3\u7801\u7ed3\u6784 <\/h3>\n<p>\u8fd8\u8bb0\u5f97\u6240\u6709\u90a3\u4e9b\u52a0\u8f7d\u3001\u9519\u8bef\u548c\u6570\u636e\u7684\u72b6\u6001\u53d8\u91cf\u5417\uff1f\u4f7f\u7528 use() \u540e\uff0c\u5b83\u4eec\u5c31\u6d88\u5931\u4e86\u3002\u60a8\u7684\u7ec4\u4ef6\u53d8\u5f97\u66f4\u52a0\u7b80\u6d01\u548c\u76f4\u63a5\u3002\u8fd9\u4e0d\u4ec5\u4ec5\u662f\u7f16\u5199\u66f4\u5c11\u7684\u4ee3\u7801\uff0c\u800c\u662f\u7f16\u5199\u66f4\u6613\u4e8e\u7ef4\u62a4\u3001\u53ef\u8bfb\u7684\u4ee3\u7801\uff0c\u4ee5\u66f4\u597d\u5730\u8868\u8fbe\u60a8\u7684\u610f\u56fe\u3002 use() \u94a9\u5b50\u6d88\u9664\u4e86\u624b\u52a8\u7f16\u6392\u52a0\u8f7d\u72b6\u6001\u548c\u9519\u8bef\u5904\u7406\u7684\u9700\u8981\uff0c\u51cf\u5c11\u4e86\u7ba1\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u8ba4\u77e5\u5f00\u9500\u3002<\/p>\n<h3> 2.\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406 <\/h3>\n<p>\u5206\u6563\u7684 try-catch \u5757\u548c\u624b\u52a8\u9519\u8bef\u72b6\u6001\u7ba1\u7406\u7684\u65e5\u5b50\u5df2\u7ecf\u4e00\u53bb\u4e0d\u590d\u8fd4\u4e86\u3002\u4f7f\u7528 use()\uff0c\u9519\u8bef\u5904\u7406\u901a\u8fc7\u9519\u8bef\u8fb9\u754c\u53d8\u5f97\u58f0\u660e\u6027\uff1a<\/p>\n<pre>errorboundary fallback={&lt;errorstate \/&gt;}&gt;\n&lt;userprofile userid={1} \/&gt;\n&lt;\/errorboundary&gt;\n<\/pre>\n<p>\u6b64\u65b9\u6cd5\u53ef\u786e\u4fdd\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u9519\u8bef\u5904\u7406\u7684\u4e00\u81f4\u6027\uff0c\u5e76\u4f7f\u9519\u8bef\u6062\u590d\u66f4\u52a0\u53ef\u9884\u6d4b\u548c\u53ef\u7ba1\u7406\u3002<\/p>\n<h3> 3. \u81ea\u52a8\u52a0\u8f7d\u72b6\u6001 <\/h3>\n<p>\u8fd8\u8bb0\u5f97\u73a9\u5f04\u52a0\u8f7d\u6807\u5fd7\u5417\uff1f use() \u94a9\u5b50\u4e0e suspense \u7ed3\u5408\uff0c\u81ea\u52a8\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<pre>&lt;Suspense fallback={&lt;LoadingSpinner \/&gt;}&gt;\n&lt;UserProfile userId={123} \/&gt;\n&lt;\/Suspense&gt;\n<\/pre>\n<p>\u8fd9\u79cd\u52a0\u8f7d\u72b6\u6001\u7684\u58f0\u660e\u6027\u65b9\u6cd5\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u521b\u5efa\u4e00\u81f4\u7684\u52a0\u8f7d\u4f53\u9a8c\u3002<\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>use() \u94a9\u5b50\u4ee3\u8868\u4e86 react \u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u91cd\u8981\u4e00\u6b65\u3002\u867d\u7136\u5b83\u9700\u8981\u6211\u4eec\u5bf9\u5e94\u7528\u7a0b\u5e8f\u7684\u601d\u8003\u548c\u7ed3\u6784\u8fdb\u884c\u4e00\u4e9b\u8c03\u6574\uff0c\u4f46\u66f4\u5e72\u51c0\u7684\u4ee3\u7801\u3001\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u548c\u6539\u8fdb\u7684\u52a0\u8f7d\u72b6\u6001\u7684\u597d\u5904\u4f7f\u5176\u6210\u4e3a react \u5de5\u5177\u5305\u4e2d\u5f15\u4eba\u6ce8\u76ee\u7684\u8865\u5145\u3002<\/p>\n<p>\u901a\u8fc7\u91c7\u7528\u8fd9\u79cd\u65b0\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u66f4\u53ef\u7ef4\u62a4\u3001\u66f4\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u4e14\u51cf\u5c11\u6837\u677f\u6587\u4ef6\u548c\u6f5c\u5728\u9519\u8bef\u3002\u968f\u7740 react \u751f\u6001\u7cfb\u7edf\u7ee7\u7eed\u56f4\u7ed5\u8fd9\u4e2a\u65b0\u8303\u5f0f\u53d1\u5c55\uff0c\u6211\u4eec\u53ef\u4ee5\u671f\u5f85\u770b\u5230\u66f4\u5f3a\u5927\u7684\u6a21\u5f0f\u548c\u5b9e\u8df5\u7684\u51fa\u73b0\u3002<\/p>\n<p>\u8bf7\u8bb0\u4f4f\uff0c\u867d\u7136 use() \u6302\u94a9\u53ef\u80fd\u770b\u8d77\u6765\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u53d8\u5316\uff0c\u4f46\u5b83\u6700\u7ec8\u662f\u4e3a\u4e86\u8ba9\u6211\u4eec\u4f5c\u4e3a\u5f00\u53d1\u4eba\u5458\u7684\u751f\u6d3b\u66f4\u8f7b\u677e\uff0c\u8ba9\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u66f4\u597d\u3002\u65e0\u8bba\u60a8\u662f\u5f00\u59cb\u4e00\u4e2a\u65b0\u9879\u76ee\u8fd8\u662f\u7ef4\u62a4\u73b0\u6709\u9879\u76ee\uff0c\u7406\u89e3\u548c\u91c7\u7528\u8fd9\u79cd\u6a21\u5f0f\u5bf9\u4e8e\u73b0\u4ee3 react \u5f00\u53d1\u90fd\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u6211\u4e0d\u5efa\u8bae\u5728\u751f\u4ea7\u4e2d\u4f7f\u7528\u5b83\uff0c\u56e0\u4e3a\u5b83\u4ecd\u5904\u4e8e\u5b9e\u9a8c\u9636\u6bb5\uff0c\u56e0\u6b64\u5728\u672a\u6765\u7684\u66f4\u65b0\u4e2d\u6b63\u5f0f\u91c7\u7528 react \u4e4b\u524d\uff0c\u6211\u4e0d\u4f1a\u5728\u751f\u4ea7\u4e2d\u4f7f\u7528\u5b83\uff0c\u4f46\u5b83\u5f88\u9002\u5408\u7528\u4e8e\u4e2a\u4eba\u9879\u76ee\u3002<\/p>\n<p>\u60a8\u5bf9 use() \u6302\u94a9\u6709\u4f55\u770b\u6cd5\uff1f\u60a8\u5f00\u59cb\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u5b83\u4e86\u5417\uff1f\u5728\u4e0b\u9762\u7684\u8bc4\u8bba\u4e2d\u5206\u4eab\u60a8\u7684\u7ecf\u9a8c\u548c\u60f3\u6cd5\uff01<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u6211\u5bf9 use() \u94a9\u5b50\u7684\u601d\u8003\u2014\u2014\u6df1\u5165\u63a2\u8ba8 React \u7684\u6700\u65b0\u5b9e\u9a8c\u529f\u80fd\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u5728 4 \u4e2d\u4f7f\u7528 Laravel Pennant \u7ba1\u7406\u529f\u80fd\u6807\u5fd7\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u5bf9 use() \u94a9\u5b50\u7684\u601d\u8003\u2014\u2014\u6df1\u5165\u63a2\u8ba8 React \u7684\u6700\u65b0\u5b9e\u9a8c\u529f\u80fd \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300a\u6211\u5bf9 use() \u94a9\u5b50\u7684\u601d\u8003\u2014\u2014\u6df1\u5165\u63a2\u8ba8 React \u7684\u6700\u65b0\u5b9e\u9a8c\u529f\u80fd\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 react 19 \u5df2\u7ecf\u53d1\u5e03\u4e86\uff0c\u5b83\u5e26\u6765\u4e86\u8bb8\u591a\u65b0\u529f\u80fd\uff0c\u4f8b\u5982\u670d\u52a1\u5668\u7ec4\u4ef6\u3001\u6307\u4ee4\uff08\u4f7f\u7528\u5ba2\u6237\u7aef\u548c\u4f7f\u7528\u670d\u52a1\u5668\uff09\u3001\u65b0\u94a9\u5b50\uff08\u4f8b\u5982 useoptimistic()\u3001useformstatus() \u548c\u5b9e\u9a8c\u6027 use()\uff09 hook\uff0c\u8fd9\u5c31\u662f\u6211\u4eca\u5929\u8981\u8bb2\u7684\u5185\u5bb9\u3002 \u4ec0\u4e48\u662f use() \u6302\u94a9\uff1f use() \u94a9\u5b50\u662f\u4e00\u9879\u65b0\u529f\u80fd\uff0c\u53ef\u8ba9\u60a8\u76f4\u63a5\u5728\u7ec4\u4ef6\u4e2d\u5904\u7406 promise\u3002\u5b83\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u5728\u7ec4\u4ef6\u5185\u90e8\u89e3\u5f00 promise \u5e76\u66f4\u7b80\u6d01\u5730\u5904\u7406\u5f02\u6b65\u6570\u636e\u7684\u65b9\u6cd5\u3002 import { use } from &#8216;react&#8217;; \/\/ example async function async function fetchuserdetails(id) { const response = await fetch(`localhost:3000\/api\/users\/${id}`); return response.json(); } function userprofile({ id }) { \/\/ use() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53200","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53200"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53200\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}