{"id":208,"date":"2026-07-17T09:46:36","date_gmt":"2026-07-17T01:46:36","guid":{"rendered":"https:\/\/yykcj.com\/?p=208"},"modified":"2026-07-17T09:46:36","modified_gmt":"2026-07-17T01:46:36","slug":"react-19-%e6%96%b0%e7%89%b9%e6%80%a7%e5%ae%9e%e6%88%98%ef%bc%9auseoptimistic%e3%80%81useactionstate-%e4%b8%8e-server-components-%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=208","title":{"rendered":"React 19 \u65b0\u7279\u6027\u5b9e\u6218\uff1auseOptimistic\u3001useActionState \u4e0e Server Components \u6df1\u5ea6\u89e3\u6790"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>React 19 \u7ec8\u4e8e\u6765\u4e86\uff01\u867d\u7136\u5b83\u6700\u521d\u88ab\u79f0\u4e3a\u201cReact 18.3\u201d\uff0c\u4f46\u6700\u7ec8\u4ee5 19 \u7684\u7248\u672c\u53f7\u53d1\u5e03\uff0c\u5e26\u6765\u4e86\u8bb8\u591a\u4ee4\u4eba\u5174\u594b\u7684\u65b0\u7279\u6027\u3002\u672c\u6587\u5c06\u901a\u8fc7\u4e09\u4e2a\u5b9e\u6218\u573a\u666f\uff0c\u5e26\u4f60\u5feb\u901f\u638c\u63e1 React 19 \u7684\u6838\u5fc3\u65b0\u529f\u80fd\uff1a<strong>useOptimistic<\/strong>\u3001<strong>useActionState<\/strong> \u4ee5\u53ca <strong>Server Components<\/strong>\u3002<\/p>\n<h2>1. useOptimistic\uff1a\u8ba9 UI \u5373\u65f6\u54cd\u5e94<\/h2>\n<h3>\u573a\u666f\uff1a\u70b9\u8d5e\u529f\u80fd<\/h3>\n<p>\u5728\u4f20\u7edf\u5b9e\u73b0\u4e2d\uff0c\u7528\u6237\u70b9\u51fb\u70b9\u8d5e\u6309\u94ae\u540e\uff0c\u9700\u8981\u7b49\u5f85 API \u54cd\u5e94\u624d\u80fd\u66f4\u65b0 UI\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u660e\u663e\u7684\u5ef6\u8fdf\u3002useOptimistic \u5141\u8bb8\u4f60\u5728 API \u8bf7\u6c42\u5b8c\u6210\u524d\u5c31\u4e50\u89c2\u5730\u66f4\u65b0 UI\uff0c\u5982\u679c\u8bf7\u6c42\u5931\u8d25\u518d\u56de\u6eda\u3002<\/p>\n<h3>\u5b9e\u6218\u4ee3\u7801<\/h3>\n<pre><code class=\"language-jsx\">\nimport { useOptimistic, useState } from &#x27;react&#x27;;\n\nfunction LikeButton({ initialLikes }) {\n  const [likes, setLikes] = useState(initialLikes);\n  const [optimisticLikes, addOptimistic] = useOptimistic(\n    likes,\n    (state, newLikes) =&gt; state + 1\n  );\n\n  const handleLike = async () =&gt; {\n    addOptimistic(1); \/\/ \u7acb\u5373\u4e50\u89c2\u66f4\u65b0\n    try {\n      const response = await fetch(&#x27;\/api\/like&#x27;, { method: &#x27;POST&#x27; });\n      const data = await response.json();\n      setLikes(data.likes); \/\/ \u7528\u771f\u5b9e\u6570\u636e\u66f4\u65b0\n    } catch {\n      \/\/ \u8bf7\u6c42\u5931\u8d25\uff0cuseOptimistic \u4f1a\u81ea\u52a8\u56de\u6eda\n    }\n  };\n\n  return (\n    &lt;button onClick={handleLike}&gt;\n      \ud83d\udc4d {optimisticLikes}\n    &lt;\/button&gt;\n  );\n}\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>useOptimistic<\/code> \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u521d\u59cb\u72b6\u6001\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u66f4\u65b0\u51fd\u6570\u3002\u66f4\u65b0\u51fd\u6570\u63a5\u6536\u5f53\u524d\u72b6\u6001\u548c\u4f20\u5165\u7684\u503c\uff0c\u8fd4\u56de\u65b0\u7684\u4e50\u89c2\u72b6\u6001\u3002\u5f53\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u6216\u5931\u8d25\u65f6\uff0cReact \u4f1a\u81ea\u52a8\u6839\u636e\u6700\u7ec8\u72b6\u6001\u8c03\u6574 UI\u3002<\/p>\n<h2>2. useActionState\uff1a\u7b80\u5316\u8868\u5355\u5904\u7406<\/h2>\n<h3>\u573a\u666f\uff1a\u767b\u5f55\u8868\u5355<\/h3>\n<p><code>useActionState<\/code> \u662f React 19 \u4e2d\u5904\u7406\u8868\u5355\u7684\u65b0 Hook\uff0c\u5b83\u7ed3\u5408\u4e86\u8868\u5355\u72b6\u6001\u7ba1\u7406\u548c\u5f02\u6b65\u64cd\u4f5c\uff0c\u7279\u522b\u9002\u5408\u4e0e Server Actions \u914d\u5408\u4f7f\u7528\u3002<\/p>\n<h3>\u5b9e\u6218\u4ee3\u7801<\/h3>\n<pre><code class=\"language-jsx\">\nimport { useActionState } from &#x27;react&#x27;;\n\nfunction LoginForm() {\n  const [state, formAction, isPending] = useActionState(\n    async (prevState, formData) =&gt; {\n      const email = formData.get(&#x27;email&#x27;);\n      const password = formData.get(&#x27;password&#x27;);\n      \n      \/\/ \u6a21\u62df API \u8c03\u7528\n      const response = await fetch(&#x27;\/api\/login&#x27;, {\n        method: &#x27;POST&#x27;,\n        body: JSON.stringify({ email, password }),\n      });\n      \n      if (!response.ok) {\n        return { error: &#x27;\u767b\u5f55\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u90ae\u7bb1\u548c\u5bc6\u7801&#x27; };\n      }\n      \n      return { success: true };\n    },\n    { error: null, success: false }\n  );\n\n  return (\n    &lt;form action={formAction}&gt;\n      &lt;input name=&quot;email&quot; type=&quot;email&quot; required \/&gt;\n      &lt;input name=&quot;password&quot; type=&quot;password&quot; required \/&gt;\n      &lt;button type=&quot;submit&quot; disabled={isPending}&gt;\n        {isPending ? &#x27;\u767b\u5f55\u4e2d...&#x27; : &#x27;\u767b\u5f55&#x27;}\n      &lt;\/button&gt;\n      {state.error &amp;&amp; &lt;p style={{ color: &#x27;red&#x27; }}&gt;{state.error}&lt;\/p&gt;}\n      {state.success &amp;&amp; &lt;p&gt;\u767b\u5f55\u6210\u529f\uff01&lt;\/p&gt;}\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<p><strong>\ud83d\udca1 \u4f7f\u7528\u6280\u5de7<\/strong>\uff1a<code>useActionState<\/code> \u7684\u7b2c\u4e09\u4e2a\u8fd4\u56de\u503c <code>isPending<\/code> \u53ef\u4ee5\u8f7b\u677e\u63a7\u5236\u6309\u94ae\u7684\u52a0\u8f7d\u72b6\u6001\u3002\u6ce8\u610f\u8868\u5355\u7684 <code>action<\/code> \u5c5e\u6027\u76f4\u63a5\u7ed1\u5b9a\u5230 <code>formAction<\/code>\uff0c\u4e0d\u518d\u9700\u8981 <code>onSubmit<\/code> \u4e8b\u4ef6\u3002<\/p>\n<h2>3. Server Components\uff1a\u63d0\u5347\u6027\u80fd\u7684\u65b0\u8303\u5f0f<\/h2>\n<h3>\u573a\u666f\uff1a\u5c55\u793a\u6587\u7ae0\u5217\u8868<\/h3>\n<p>Server Components \u5141\u8bb8\u4f60\u5728\u670d\u52a1\u7aef\u6e32\u67d3\u7ec4\u4ef6\uff0c\u51cf\u5c11\u5ba2\u6237\u7aef JavaScript \u4f53\u79ef\uff0c\u63d0\u5347\u9996\u5c4f\u52a0\u8f7d\u901f\u5ea6\u3002<\/p>\n<h3>\u5b9e\u6218\u4ee3\u7801<\/h3>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u9879\u76ee\u652f\u6301 React 19 \u548c Next.js 14+\u3002\u521b\u5efa\u4e00\u4e2a Server Component\uff1a<\/p>\n<pre><code class=\"language-jsx\">\n\/\/ app\/articles\/page.jsx (Server Component)\nimport ArticleCard from &#x27;.\/ArticleCard&#x27;; \/\/ Client Component\n\nexport default async function ArticlesPage() {\n  const res = await fetch(&#x27;https:\/\/api.example.com\/articles&#x27;);\n  const articles = await res.json();\n\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;\u6700\u65b0\u6587\u7ae0&lt;\/h1&gt;\n      {articles.map(article =&gt; (\n        &lt;ArticleCard key={article.id} article={article} \/&gt;\n      ))}\n    &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n<p>\u800c <code>ArticleCard<\/code> \u53ef\u4ee5\u662f\u4e00\u4e2a Client Component\uff08\u5982\u679c\u9700\u8981\u4ea4\u4e92\uff09\uff1a<\/p>\n<pre><code class=\"language-jsx\">\n\/\/ app\/articles\/ArticleCard.jsx (Client Component)\n&#x27;use client&#x27;;\n\nexport default function ArticleCard({ article }) {\n  const [liked, setLiked] = useState(false);\n\n  return (\n    &lt;div&gt;\n      &lt;h2&gt;{article.title}&lt;\/h2&gt;\n      &lt;p&gt;{article.summary}&lt;\/p&gt;\n      &lt;button onClick={() =&gt; setLiked(!liked)}&gt;\n        {liked ? &#x27;\u2764\ufe0f&#x27; : &#x27;\ud83e\udd0d&#x27;}\n      &lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n<p><strong>\u5173\u952e\u70b9<\/strong>\uff1a<\/p>\n<ul>\n<li>Server Components \u9ed8\u8ba4\u662f\u670d\u52a1\u7aef\u6e32\u67d3\uff0c\u4e0d\u80fd\u4f7f\u7528 <code>useState<\/code>\u3001<code>useEffect<\/code> \u7b49\u5ba2\u6237\u7aef Hook\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u4ea4\u4e92\u529f\u80fd\uff0c\u6dfb\u52a0 <code>'use client'<\/code> \u6307\u4ee4\u5c06\u5176\u6807\u8bb0\u4e3a Client Component\u3002<\/li>\n<li>\u6df7\u5408\u4f7f\u7528 Server \u548c Client Components \u53ef\u4ee5\u6700\u5927\u5316\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2>\u5e38\u89c1\u5751\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<ol>\n<li><strong>useOptimistic \u7684\u56de\u6eda\u673a\u5236<\/strong>\uff1a\u4e50\u89c2\u66f4\u65b0\u540e\uff0c\u5982\u679c\u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\uff0cReact \u4f1a\u81ea\u52a8\u5c06\u72b6\u6001\u56de\u6eda\u5230\u4e0a\u4e00\u6b21\u6b63\u786e\u7684\u503c\u3002\u4f46\u8981\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u7684\u66f4\u65b0\u51fd\u6570\u6709\u526f\u4f5c\u7528\uff08\u5982\u66f4\u65b0\u672c\u5730\u5b58\u50a8\uff09\uff0c\u9700\u8981\u624b\u52a8\u5904\u7406\u56de\u6eda\u3002<\/li>\n<\/ol>\n<ol>\n<li><strong>useActionState \u7684\u521d\u59cb\u503c<\/strong>\uff1a\u521d\u59cb\u72b6\u6001\u5bf9\u8c61\u7684\u7ed3\u6784\u8981\u4e0e\u5f02\u6b65\u51fd\u6570\u8fd4\u56de\u7684\u7ed3\u6784\u4e00\u81f4\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u7c7b\u578b\u9519\u8bef\u3002<\/li>\n<\/ol>\n<ol>\n<li><strong>Server Components \u7684\u6570\u636e\u83b7\u53d6<\/strong>\uff1a\u5728 Server Components \u4e2d\u76f4\u63a5 <code>await fetch<\/code> \u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5b83\u53ea\u5728\u670d\u52a1\u7aef\u6267\u884c\u3002\u4f46\u8981\u6ce8\u610f\u9519\u8bef\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>try\/catch<\/code> \u6216 React \u7684 <code>error.js<\/code> \u8fb9\u754c\u3002<\/li>\n<\/ol>\n<h2>\u603b\u7ed3<\/h2>\n<p>React 19 \u7684\u8fd9\u4e09\u4e2a\u65b0\u7279\u6027\u663e\u8457\u63d0\u5347\u4e86\u5f00\u53d1\u4f53\u9a8c\u548c\u6027\u80fd\uff1a<\/p>\n<ul>\n<li><strong>useOptimistic<\/strong> \u8ba9 UI \u5373\u65f6\u54cd\u5e94\uff0c\u63d0\u5347\u7528\u6237\u611f\u77e5\u3002<\/li>\n<li><strong>useActionState<\/strong> \u7b80\u5316\u4e86\u8868\u5355\u72b6\u6001\u7ba1\u7406\uff0c\u4e0e Server Actions \u5b8c\u7f8e\u914d\u5408\u3002<\/li>\n<li><strong>Server Components<\/strong> \u5e26\u6765\u4e86\u670d\u52a1\u7aef\u6e32\u67d3\u7684\u65b0\u8303\u5f0f\uff0c\u51cf\u5c11\u5ba2\u6237\u7aef\u8d1f\u62c5\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u8fd9\u4e9b\u7279\u6027\u6574\u5408\u5230\u73b0\u6709\u9879\u76ee\u4e2d\uff0c\u6216\u8005\u63a2\u7d22 React 19 \u7684\u5176\u4ed6\u65b0\u529f\u80fd\uff0c\u5982 <code>use()<\/code> API \u548c\u6587\u6863\u5143\u6570\u636e\u7ba1\u7406\u3002<\/p>\n<h2>\u53c2\u8003\u8d44\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/react.dev\/blog\/2024\/12\/05\/react-19\">React 19 \u5b98\u65b9\u53d1\u5e03\u516c\u544a<\/a><\/li>\n<li><a href=\"https:\/\/react.dev\/blog\/2024\/12\/05\/react-19#whats-new-in-react-19\">React 19 \u65b0\u7279\u6027\u5217\u8868<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>React 19 \u6b63\u5f0f\u53d1\u5e03\uff0c\u5e26\u6765\u4e86 useOptimistic\u3001useActionState \u7b49\u5168\u65b0 Hook \u4ee5\u53ca Server Components \u7684\u7a33\u5b9a\u652f\u6301\u3002\u672c\u6587\u901a\u8fc7\u4e09\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u624b\u628a\u624b\u6559\u4f60\u5982\u4f55\u5728\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u7279\u6027\uff0c\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[139,142,141,140,36,143],"class_list":["post-208","post","type-post","status-publish","format-standard","hentry","category-3","tag-react-19","tag-server-components","tag-useactionstate","tag-useoptimistic","tag-36","tag-143"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=208"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}