{"id":193,"date":"2026-07-03T09:57:57","date_gmt":"2026-07-03T01:57:57","guid":{"rendered":"https:\/\/yykcj.com\/?p=193"},"modified":"2026-07-03T09:57:57","modified_gmt":"2026-07-03T01:57:57","slug":"react-server-actions-%e5%85%a5%e9%97%a8%e4%b8%8e%e8%bf%9b%e9%98%b6%ef%bc%9a%e4%bb%8e%e8%a1%a8%e5%8d%95%e6%8f%90%e4%ba%a4%e5%88%b0%e6%95%b0%e6%8d%ae%e7%aa%81%e5%8f%98%e7%9a%84%e6%9c%80%e4%bd%b3","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=193","title":{"rendered":"React Server Actions \u5165\u95e8\u4e0e\u8fdb\u9636\uff1a\u4ece\u8868\u5355\u63d0\u4ea4\u5230\u6570\u636e\u7a81\u53d8\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>React Server Actions \u662f React 19 \u5f15\u5165\u7684\u4e00\u9879\u9769\u547d\u6027\u7279\u6027\uff0c\u5141\u8bb8\u76f4\u63a5\u5728\u670d\u52a1\u5668\u7aef\u6267\u884c\u51fd\u6570\uff0c\u65e0\u7f1d\u5904\u7406\u6570\u636e\u7a81\u53d8\uff08mutations\uff09\u3002\u5b83\u7b80\u5316\u4e86\u4f20\u7edf\u7684\u524d\u540e\u7aef\u4ea4\u4e92\u6a21\u5f0f\uff0c\u5c24\u5176\u9002\u5408\u8868\u5355\u63d0\u4ea4\u3001\u6570\u636e\u66f4\u65b0\u7b49\u573a\u666f\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u96f6\u5f00\u59cb\uff0c\u638c\u63e1 Server Actions \u7684\u6838\u5fc3\u7528\u6cd5\u3001\u6700\u4f73\u5b9e\u8df5\u4ee5\u53ca\u5e38\u89c1\u9677\u9631\uff0c\u8ba9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u6e38\u5203\u6709\u4f59\u3002<\/p>\n<h2>\u73af\u5883\u51c6\u5907<\/h2>\n<p>\u786e\u4fdd\u4f60\u7684\u9879\u76ee\u4f7f\u7528 React 19 \u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u5e76\u642d\u914d Next.js 14+\uff08App Router\uff09\u6216\u652f\u6301 Server Actions \u7684\u6846\u67b6\u3002\u672c\u6587\u793a\u4f8b\u57fa\u4e8e Next.js 14\u3002<\/p>\n<pre><code class=\"language-bash\">\nnpx create-next-app@latest my-app --typescript\ncd my-app\n<\/code><\/pre>\n<h2>\u57fa\u7840\u5165\u95e8\uff1a\u7b2c\u4e00\u4e2a Server Action<\/h2>\n<h3>1. \u5b9a\u4e49 Action<\/h3>\n<p>\u5728 Next.js \u4e2d\uff0cServer Actions \u53ef\u4ee5\u5b9a\u4e49\u5728\u6587\u4ef6\u9876\u90e8\uff08\u4f7f\u7528 <code>\"use server\"<\/code> \u6307\u4ee4\uff09\u6216\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u3002\u6700\u7b80\u5355\u7684\u5f62\u5f0f\uff1a<\/p>\n<pre><code class=\"language-tsx\">\n\/\/ app\/actions.ts\n&quot;use server&quot;;\n\nexport async function createUser(formData: FormData) {\n  const name = formData.get(&quot;name&quot;) as string;\n  const email = formData.get(&quot;email&quot;) as string;\n\n  \/\/ \u6a21\u62df\u6570\u636e\u5e93\u64cd\u4f5c\n  console.log(&quot;Creating user:&quot;, { name, email });\n  return { success: true };\n}\n<\/code><\/pre>\n<h3>2. \u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/h3>\n<pre><code class=\"language-tsx\">\n\/\/ app\/page.tsx\n&quot;use client&quot;;\n\nimport { createUser } from &quot;.\/actions&quot;;\n\nexport default function Home() {\n  return (\n    &lt;form action={createUser}&gt;\n      &lt;input name=&quot;name&quot; placeholder=&quot;Name&quot; required \/&gt;\n      &lt;input name=&quot;email&quot; type=&quot;email&quot; placeholder=&quot;Email&quot; required \/&gt;\n      &lt;button type=&quot;submit&quot;&gt;Submit&lt;\/button&gt;\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<p>> \ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>action<\/code> \u5c5e\u6027\u76f4\u63a5\u63a5\u6536 Server Action \u51fd\u6570\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u7528 <code>fetch<\/code> \u6216 <code>axios<\/code>\u3002\u8868\u5355\u63d0\u4ea4\u4f1a\u81ea\u52a8\u53d1\u9001\u8bf7\u6c42\u5e76\u5904\u7406\u54cd\u5e94\u3002<\/p>\n<h2>\u8fdb\u9636\u7528\u6cd5\uff1a\u6570\u636e\u9a8c\u8bc1\u4e0e\u9519\u8bef\u5904\u7406<\/h2>\n<h3>1. \u4f7f\u7528 Zod \u8fdb\u884c\u9a8c\u8bc1<\/h3>\n<pre><code class=\"language-tsx\">\n&quot;use server&quot;;\n\nimport { z } from &quot;zod&quot;;\n\nconst schema = z.object({\n  name: z.string().min(2, &quot;Name must be at least 2 characters&quot;),\n  email: z.string().email(&quot;Invalid email&quot;),\n});\n\nexport async function createUser(formData: FormData) {\n  const validated = schema.safeParse({\n    name: formData.get(&quot;name&quot;),\n    email: formData.get(&quot;email&quot;),\n  });\n\n  if (!validated.success) {\n    return { errors: validated.error.flatten().fieldErrors };\n  }\n\n  \/\/ \u5904\u7406\u6709\u6548\u6570\u636e\n  console.log(&quot;Validated data:&quot;, validated.data);\n  return { success: true };\n}\n<\/code><\/pre>\n<h3>2. \u5ba2\u6237\u7aef\u5c55\u793a\u9519\u8bef<\/h3>\n<pre><code class=\"language-tsx\">\n&quot;use client&quot;;\n\nimport { createUser } from &quot;.\/actions&quot;;\nimport { useActionState } from &quot;react&quot;;\n\nexport default function Home() {\n  const [state, formAction] = useActionState(createUser, { errors: {} });\n\n  return (\n    &lt;form action={formAction}&gt;\n      &lt;input name=&quot;name&quot; placeholder=&quot;Name&quot; \/&gt;\n      {state.errors?.name &amp;&amp; &lt;p style={{ color: &quot;red&quot; }}&gt;{state.errors.name}&lt;\/p&gt;}\n      &lt;input name=&quot;email&quot; type=&quot;email&quot; placeholder=&quot;Email&quot; \/&gt;\n      {state.errors?.email &amp;&amp; &lt;p style={{ color: &quot;red&quot; }}&gt;{state.errors.email}&lt;\/p&gt;}\n      &lt;button type=&quot;submit&quot;&gt;Submit&lt;\/button&gt;\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<p>> \ud83d\udca1 <strong>\u63d0\u793a<\/strong>\uff1a<code>useActionState<\/code>\uff08React 19 \u65b0\u589e\uff09\u53ef\u4ee5\u7ba1\u7406 Action \u7684\u72b6\u6001\uff0c\u5305\u62ec\u9519\u8bef\u548c\u52a0\u8f7d\u72b6\u6001\u3002<\/p>\n<h2>\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e0e Server Actions \u7684\u4ea4\u4e92<\/h2>\n<h3>1. \u8c03\u7528 Server Action \u5e76\u5237\u65b0\u6570\u636e<\/h3>\n<pre><code class=\"language-tsx\">\n&quot;use client&quot;;\n\nimport { revalidatePath } from &quot;next\/cache&quot;;\nimport { deleteUser } from &quot;.\/actions&quot;;\n\nexport function DeleteButton({ userId }: { userId: string }) {\n  const handleDelete = async () =&gt; {\n    await deleteUser(userId);\n    \/\/ \u6ce8\u610f\uff1a\u4e0d\u80fd\u76f4\u63a5\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u8c03\u7528 revalidatePath\uff0c\u9700\u8981\u4ece Action \u5185\u90e8\u89e6\u53d1\n  };\n\n  return &lt;button onClick={handleDelete}&gt;Delete&lt;\/button&gt;;\n}\n<\/code><\/pre>\n<p>\u6b63\u786e\u505a\u6cd5\uff1a\u5728 Server Action \u4e2d\u8c03\u7528 <code>revalidatePath<\/code>\u3002<\/p>\n<pre><code class=\"language-tsx\">\n&quot;use server&quot;;\n\nimport { revalidatePath } from &quot;next\/cache&quot;;\n\nexport async function deleteUser(userId: string) {\n  \/\/ \u5220\u9664\u7528\u6237\u903b\u8f91\n  revalidatePath(&quot;\/users&quot;); \/\/ \u91cd\u65b0\u83b7\u53d6\u7528\u6237\u5217\u8868\n  return { success: true };\n}\n<\/code><\/pre>\n<h3>2. \u4f20\u9012\u975e\u8868\u5355\u6570\u636e<\/h3>\n<p>\u5982\u679c Action \u9700\u8981\u63a5\u6536\u975e\u8868\u5355\u6570\u636e\uff08\u5982 JSON \u5bf9\u8c61\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>bind<\/code> \u6216\u76f4\u63a5\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"language-tsx\">\n&quot;use client&quot;;\n\nimport { updateUser } from &quot;.\/actions&quot;;\n\nexport function UpdateButton({ user }: { user: { id: string; name: string } }) {\n  const handleUpdate = async () =&gt; {\n    await updateUser(user);\n  };\n\n  return &lt;button onClick={handleUpdate}&gt;Update&lt;\/button&gt;;\n}\n<\/code><\/pre>\n<pre><code class=\"language-tsx\">\n&quot;use server&quot;;\n\nexport async function updateUser(user: { id: string; name: string }) {\n  \/\/ \u66f4\u65b0\u903b\u8f91\n}\n<\/code><\/pre>\n<h2>\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<h3>1. \u907f\u514d\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u76f4\u63a5\u4fee\u6539\u72b6\u6001<\/h3>\n<p>Server Actions \u662f\u5f02\u6b65\u7684\uff0c\u4e0d\u8981\u5728 <code>onClick<\/code> \u4e2d\u76f4\u63a5\u8c03\u7528\u5e76\u671f\u671b\u7acb\u5373\u66f4\u65b0\u72b6\u6001\u3002\u4f7f\u7528 <code>useActionState<\/code> \u6216 <code>startTransition<\/code> \u6765\u7ba1\u7406\u3002<\/p>\n<h3>2. \u5b89\u5168\u6027\uff1a\u6c38\u8fdc\u4e0d\u8981\u4fe1\u4efb\u5ba2\u6237\u7aef\u6570\u636e<\/h3>\n<p>\u59cb\u7ec8\u5728 Server Action \u4e2d\u8fdb\u884c\u9a8c\u8bc1\u548c\u6388\u6743\u68c0\u67e5\u3002<\/p>\n<pre><code class=\"language-tsx\">\n&quot;use server&quot;;\n\nexport async function deleteUser(userId: string) {\n  const session = await getSession();\n  if (!session || session.user.id !== userId) {\n    throw new Error(&quot;Unauthorized&quot;);\n  }\n  \/\/ \u5220\u9664\u903b\u8f91\n}\n<\/code><\/pre>\n<h3>3. \u4f7f\u7528 <code>useActionState<\/code> \u5904\u7406\u52a0\u8f7d\u72b6\u6001<\/h3>\n<pre><code class=\"language-tsx\">\nconst [state, formAction, isPending] = useActionState(createUser, { errors: {} });\n\nreturn (\n  &lt;form action={formAction}&gt;\n    &lt;button disabled={isPending}&gt;{isPending ? &quot;Submitting...&quot; : &quot;Submit&quot;}&lt;\/button&gt;\n  &lt;\/form&gt;\n);\n<\/code><\/pre>\n<h3>4. \u9519\u8bef\u8fb9\u754c<\/h3>\n<p>\u5bf9\u4e8e\u672a\u6355\u83b7\u7684\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528 React \u9519\u8bef\u8fb9\u754c\u6216 Next.js \u7684 <code>error.js<\/code> \u6587\u4ef6\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>React Server Actions \u6781\u5927\u7b80\u5316\u4e86\u6570\u636e\u7a81\u53d8\u6d41\u7a0b\uff0c\u8ba9\u524d\u7aef\u5f00\u53d1\u8005\u65e0\u9700\u624b\u52a8\u7ba1\u7406 API \u8def\u7531\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u4f60\u5e94\u8be5\u638c\u63e1\u4e86\uff1a<\/p>\n<ul>\n<li>\u5b9a\u4e49\u548c\u4f7f\u7528 Server Actions<\/li>\n<li>\u7ed3\u5408 Zod \u8fdb\u884c\u6570\u636e\u9a8c\u8bc1<\/li>\n<li>\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u5904\u7406\u72b6\u6001\u548c\u9519\u8bef<\/li>\n<li>\u8c03\u7528 Server Actions \u5e76\u89e6\u53d1\u7f13\u5b58\u5237\u65b0<\/li>\n<li>\u5b89\u5168\u6027\u548c\u6027\u80fd\u6700\u4f73\u5b9e\u8df5<\/li>\n<\/ul>\n<p>\u4e0b\u4e00\u6b65\uff0c\u53ef\u4ee5\u63a2\u7d22 Server Actions \u4e0e Streaming\u3001Suspense \u7684\u7ed3\u5408\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u5927\u578b\u9879\u76ee\u4e2d\u7ec4\u7ec7 Action \u4ee3\u7801\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/react.dev\/reference\/rsc\/server-actions\">React \u5b98\u65b9\u6587\u6863 &#8211; Server Actions<\/a><\/li>\n<li><a href=\"https:\/\/nextjs.org\/docs\/app\/building-your-application\/data-fetching\/server-actions-and-mutations\">Next.js \u6587\u6863 &#8211; Server Actions<\/a><\/li>\n<li><a href=\"https:\/\/zod.dev\/\">Zod \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u89e3\u6790 React Server Actions \u7684\u6838\u5fc3\u6982\u5ff5\u4e0e\u5b9e\u6218\u6280\u5de7\uff0c\u6db5\u76d6\u8868\u5355\u5904\u7406\u3001\u6570\u636e\u9a8c\u8bc1\u3001\u9519\u8bef\u5904\u7406\u53ca\u4e0e\u5ba2\u6237\u7aef\u7ec4\u4ef6\u7684\u4ea4\u4e92\uff0c\u52a9\u4f60\u6784\u5efa\u9ad8\u6548\u7684\u5168\u6808 React \u5e94\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[97,11,96,99,45,98],"class_list":["post-193","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-next-js","tag-react","tag-server-actions","tag-99","tag-45","tag-98"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/193","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=193"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}