{"id":249,"date":"2026-09-11T10:01:49","date_gmt":"2026-09-11T02:01:49","guid":{"rendered":"https:\/\/yykcj.com\/?p=249"},"modified":"2026-09-11T10:01:49","modified_gmt":"2026-09-11T02:01:49","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%e4%b9%90%e8%a7%82%e6%9b%b4%e6%96%b0%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=249","title":{"rendered":"React Server Actions \u5165\u95e8\u4e0e\u8fdb\u9636\uff1a\u4ece\u8868\u5355\u63d0\u4ea4\u5230\u4e50\u89c2\u66f4\u65b0\u5b9e\u6218"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u9700\u8981 Server Actions\uff1f<\/h2>\n<p>\u5728\u4f20\u7edf\u7684 React \u5e94\u7528\u4e2d\uff0c\u524d\u540e\u7aef\u4ea4\u4e92\u901a\u5e38\u9700\u8981\u624b\u52a8\u521b\u5efa API \u8def\u7531\u3001\u7f16\u5199 fetch \u8bf7\u6c42\u3001\u5904\u7406\u52a0\u8f7d\u548c\u9519\u8bef\u72b6\u6001\u3002\u8fd9\u4e0d\u4ec5\u7e41\u7410\uff0c\u8fd8\u5bb9\u6613\u5bfc\u81f4\u4ee3\u7801\u91cd\u590d\u548c\u7c7b\u578b\u4e0d\u5b89\u5168\u3002React Server Actions\uff08RSC Actions\uff09\u5141\u8bb8\u4f60\u5728\u7ec4\u4ef6\u4e2d\u76f4\u63a5\u5b9a\u4e49\u5f02\u6b65\u51fd\u6570\uff0c\u5728\u670d\u52a1\u5668\u7aef\u8fd0\u884c\uff0c\u5e76\u901a\u8fc7\u7f51\u7edc\u8c03\u7528\uff0c\u4ece\u800c\u7b80\u5316\u6570\u636e\u53d8\u66f4\u903b\u8f91\u3002<\/p>\n<p>Server Actions \u662f React 18\/19 \u4e2d\u4e0e Server Components \u914d\u5408\u7684\u91cd\u8981\u7279\u6027\uff0c\u5b83\u8ba9\u4f60\u80fd\u50cf\u8c03\u7528\u672c\u5730\u51fd\u6570\u4e00\u6837\u8c03\u7528\u670d\u52a1\u5668\u51fd\u6570\uff0c\u540c\u65f6\u4fdd\u6301\u6e10\u8fdb\u589e\u5f3a\u548c\u7c7b\u578b\u5b89\u5168\u3002\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb\uff0c\u5e26\u4f60\u638c\u63e1 Server Actions \u7684\u6838\u5fc3\u7528\u6cd5\uff0c\u5e76\u6df1\u5165\u5b9e\u6218\u573a\u666f\u3002<\/p>\n<h2>\u73af\u5883\u51c6\u5907<\/h2>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u4f7f\u7528\u652f\u6301 Server Actions \u7684\u6846\u67b6\uff0c\u4f8b\u5982 Next.js 14+\uff08App Router\uff09\u6216 Remix\u3002\u672c\u6587\u4ee5 Next.js 14 \u4e3a\u4f8b\u3002<\/p>\n<pre><code class=\"language-bash\">\nnpx create-next-app@latest my-app --typescript --app\ncd my-app\n<\/code><\/pre>\n<p>\u5728 <code>next.config.js<\/code> \u4e2d\u542f\u7528\u5b9e\u9a8c\u6027\u529f\u80fd\uff08\u82e5\u4f7f\u7528 Next.js 14\uff0c\u9ed8\u8ba4\u5df2\u542f\u7528\uff09\uff1a<\/p>\n<pre><code class=\"language-js\">\n\/** @type {import(&#x27;next&#x27;).NextConfig} *\/\nconst nextConfig = {\n  experimental: {\n    serverActions: true,\n  },\n};\n\nmodule.exports = nextConfig;\n<\/code><\/pre>\n<h2>\u7b2c\u4e00\u6b65\uff1a\u521b\u5efa\u7b2c\u4e00\u4e2a Server Action<\/h2>\n<p>Server Action \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u4f7f\u7528 <code>'use server'<\/code> \u6307\u4ee4\u6807\u8bb0\u3002\u5b83\u53ef\u4ee5\u5728\u670d\u52a1\u7aef\u7ec4\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u4e5f\u53ef\u4ee5\u5728\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002<\/p>\n<p>\u5728 <code>app\/actions.ts<\/code> \u4e2d\uff1a<\/p>\n<pre><code class=\"language-ts\">\n&#x27;use server&#x27;;\n\nexport async function createTodo(formData: FormData) {\n  const title = formData.get(&#x27;title&#x27;) as string;\n  if (!title || title.trim().length === 0) {\n    throw new Error(&#x27;Title is required&#x27;);\n  }\n  \/\/ \u6a21\u62df\u6570\u636e\u5e93\u63d2\u5165\n  await new Promise((resolve) =&gt; setTimeout(resolve, 1000));\n  console.log(&#x27;Todo created:&#x27;, title);\n  return { success: true };\n}\n<\/code><\/pre>\n<p>\u5728\u670d\u52a1\u7aef\u7ec4\u4ef6\u4e2d\u76f4\u63a5\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-tsx\">\n\/\/ app\/page.tsx\nimport { createTodo } from &#x27;.\/actions&#x27;;\n\nexport default function Home() {\n  return (\n    &lt;form action={createTodo}&gt;\n      &lt;input type=&quot;text&quot; name=&quot;title&quot; placeholder=&quot;Enter todo&quot; \/&gt;\n      &lt;button type=&quot;submit&quot;&gt;Add&lt;\/button&gt;\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<p>\u5f53\u8868\u5355\u63d0\u4ea4\u65f6\uff0cReact \u4f1a\u81ea\u52a8\u8c03\u7528 <code>createTodo<\/code>\uff0c\u5e76\u5c06\u8868\u5355\u6570\u636e\u4f5c\u4e3a <code>FormData<\/code> \u4f20\u5165\u3002\u65e0\u9700\u624b\u52a8 <code>fetch<\/code>\uff0c\u4e5f\u65e0\u9700\u5b9a\u4e49 API \u8def\u7531\u3002<\/p>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1aServer Action \u5fc5\u987b\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u4e14\u53ea\u80fd\u5728\u670d\u52a1\u7aef\u7ec4\u4ef6\u6216\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u901a\u8fc7 <code>action<\/code> \u5c5e\u6027\u6216 <code>startTransition<\/code> \u8c03\u7528\u3002<\/p>\n<h2>\u7b2c\u4e8c\u6b65\uff1a\u5904\u7406\u9519\u8bef\u4e0e\u52a0\u8f7d\u72b6\u6001<\/h2>\n<p>\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>useFormStatus<\/code> \u83b7\u53d6\u8868\u5355\u63d0\u4ea4\u72b6\u6001\uff0c\u6216\u4f7f\u7528 <code>useActionState<\/code>\uff08React 19\uff09\u7ba1\u7406\u8fd4\u56de\u503c\u548c\u9519\u8bef\u3002<\/p>\n<pre><code class=\"language-tsx\">\n&#x27;use client&#x27;;\nimport { useFormStatus } from &#x27;react-dom&#x27;;\nimport { createTodo } from &#x27;.\/actions&#x27;;\n\nfunction SubmitButton() {\n  const { pending } = useFormStatus();\n  return (\n    &lt;button type=&quot;submit&quot; disabled={pending}&gt;\n      {pending ? &#x27;Adding...&#x27; : &#x27;Add&#x27;}\n    &lt;\/button&gt;\n  );\n}\n\nexport function TodoForm() {\n  return (\n    &lt;form action={createTodo}&gt;\n      &lt;input type=&quot;text&quot; name=&quot;title&quot; required \/&gt;\n      &lt;SubmitButton \/&gt;\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u9519\u8bef\u5904\u7406\uff0c\u53ef\u4ee5\u7ed3\u5408 <code>useActionState<\/code>\uff08\u4ee5\u524d\u53eb <code>useFormState<\/code>\uff09\uff1a<\/p>\n<pre><code class=\"language-tsx\">\n&#x27;use client&#x27;;\nimport { useActionState } from &#x27;react&#x27;;\nimport { createTodo } from &#x27;.\/actions&#x27;;\n\nconst initialState = { error: null };\n\nexport function TodoForm() {\n  const [state, formAction] = useActionState(async (prevState, formData) =&gt; {\n    try {\n      await createTodo(formData);\n      return { error: null };\n    } catch (e) {\n      return { error: e.message };\n    }\n  }, initialState);\n\n  return (\n    &lt;form action={formAction}&gt;\n      &lt;input type=&quot;text&quot; name=&quot;title&quot; required \/&gt;\n      {state.error &amp;&amp; &lt;p style={{ color: &#x27;red&#x27; }}&gt;{state.error}&lt;\/p&gt;}\n      &lt;button type=&quot;submit&quot;&gt;Add&lt;\/button&gt;\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<h2>\u7b2c\u4e09\u6b65\uff1a\u8fdb\u9636\u2014\u2014\u4e50\u89c2\u66f4\u65b0\u4e0e\u91cd\u65b0\u9a8c\u8bc1<\/h2>\n<p>Server Actions \u5e38\u4e0e <code>useOptimistic<\/code> \u548c <code>revalidatePath<\/code> \u642d\u914d\uff0c\u5b9e\u73b0\u5373\u65f6 UI \u53cd\u9988\u548c\u6570\u636e\u5237\u65b0\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5f85\u529e\u5217\u8868\uff0c\u4ece\u6570\u636e\u5e93\u83b7\u53d6\u6570\u636e\u3002\u5728 Server Action \u4e2d\u63d2\u5165\u540e\uff0c\u8c03\u7528 <code>revalidatePath('\/')<\/code> \u8ba9\u9875\u9762\u91cd\u65b0\u83b7\u53d6\u6700\u65b0\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-ts\">\n&#x27;use server&#x27;;\nimport { revalidatePath } from &#x27;next\/cache&#x27;;\n\nexport async function addTodo(formData: FormData) {\n  const title = formData.get(&#x27;title&#x27;) as string;\n  \/\/ \u63d2\u5165\u6570\u636e\u5e93...\n  revalidatePath(&#x27;\/&#x27;);\n}\n<\/code><\/pre>\n<p>\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\uff0c\u4f7f\u7528 <code>useOptimistic<\/code> \u5b9e\u73b0\u4e50\u89c2\u66f4\u65b0\uff1a<\/p>\n<pre><code class=\"language-tsx\">\n&#x27;use client&#x27;;\nimport { useOptimistic } from &#x27;react&#x27;;\nimport { addTodo } from &#x27;.\/actions&#x27;;\n\nexport function TodoList({ todos }: { todos: string[] }) {\n  const [optimisticTodos, addOptimisticTodo] = useOptimistic(\n    todos,\n    (state, newTodo: string) =&gt; [...state, newTodo]\n  );\n\n  async function formAction(formData: FormData) {\n    const title = formData.get(&#x27;title&#x27;) as string;\n    addOptimisticTodo(title);\n    await addTodo(formData);\n  }\n\n  return (\n    &lt;div&gt;\n      &lt;form action={formAction}&gt;\n        &lt;input type=&quot;text&quot; name=&quot;title&quot; required \/&gt;\n        &lt;button type=&quot;submit&quot;&gt;Add&lt;\/button&gt;\n      &lt;\/form&gt;\n      &lt;ul&gt;\n        {optimisticTodos.map((todo, i) =&gt; (\n          &lt;li key={i}&gt;{todo}&lt;\/li&gt;\n        ))}\n      &lt;\/ul&gt;\n    &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u63d0\u4ea4\u540e\u7acb\u5373\u770b\u5230\u65b0\u5f85\u529e\uff0c\u800c\u65e0\u9700\u7b49\u5f85\u670d\u52a1\u5668\u54cd\u5e94\u3002<\/p>\n<h2>\u7b2c\u56db\u6b65\uff1a\u5b89\u5168\u6700\u4f73\u5b9e\u8df5<\/h2>\n<p>Server Actions \u76f4\u63a5\u66b4\u9732\u5728\u7f51\u7edc\u4e0a\uff0c\u5fc5\u987b\u8fdb\u884c\u5b89\u5168\u9632\u62a4\uff1a<\/p>\n<ol>\n<li><strong>\u59cb\u7ec8\u9a8c\u8bc1\u8f93\u5165<\/strong>\uff1a\u4f7f\u7528 Zod \u7b49\u5e93\u8fdb\u884c\u6a21\u5f0f\u9a8c\u8bc1\u3002<\/li>\n<li><strong>\u8ba4\u8bc1\u4e0e\u6388\u6743<\/strong>\uff1a\u5728 Action \u5185\u90e8\u68c0\u67e5\u7528\u6237\u4f1a\u8bdd\uff0c\u907f\u514d\u672a\u6388\u6743\u8bbf\u95ee\u3002<\/li>\n<li><strong>\u9650\u5236\u66b4\u9732\u7684 Action<\/strong>\uff1a\u907f\u514d\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u5bfc\u5165\u672a\u7ecf\u9a8c\u8bc1\u7684 Action\u3002<\/li>\n<\/ol>\n<p>\u793a\u4f8b\uff1a\u4f7f\u7528 Zod \u9a8c\u8bc1\u5e76\u68c0\u67e5\u8ba4\u8bc1\u3002<\/p>\n<pre><code class=\"language-ts\">\n&#x27;use server&#x27;;\nimport { z } from &#x27;zod&#x27;;\nimport { auth } from &#x27;@\/auth&#x27;; \/\/ \u5047\u8bbe\u7684\u8ba4\u8bc1\u5de5\u5177\n\nconst schema = z.object({\n  title: z.string().min(1).max(100),\n});\n\nexport async function createTodo(formData: FormData) {\n  const session = await auth();\n  if (!session) throw new Error(&#x27;Unauthorized&#x27;);\n\n  const parsed = schema.safeParse({ title: formData.get(&#x27;title&#x27;) });\n  if (!parsed.success) throw new Error(&#x27;Invalid input&#x27;);\n\n  \/\/ \u5b89\u5168\u5730\u4f7f\u7528 parsed.data.title\n}\n<\/code><\/pre>\n<h2>\u5e38\u89c1\u5751\u4e0e\u6027\u80fd\u4f18\u5316<\/h2>\n<ul>\n<li><strong>\u4e0d\u8981\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u76f4\u63a5\u5b9a\u4e49 Server Action<\/strong>\uff1a\u5fc5\u987b\u4f7f\u7528 <code>'use server'<\/code> \u6307\u4ee4\uff0c\u4e14\u51fd\u6570\u5fc5\u987b\u662f\u5f02\u6b65\u7684\u3002<\/li>\n<li><strong>\u907f\u514d\u5728 Action \u4e2d\u6267\u884c\u957f\u65f6\u95f4\u4efb\u52a1<\/strong>\uff1aServer Action \u9ed8\u8ba4\u6709\u8d85\u65f6\u9650\u5236\uff0c\u957f\u65f6\u95f4\u4efb\u52a1\u5e94\u4f7f\u7528\u540e\u53f0\u4f5c\u4e1a\u3002<\/li>\n<li><strong>\u5408\u7406\u4f7f\u7528 revalidatePath \u548c revalidateTag<\/strong>\uff1a\u8fc7\u5ea6\u91cd\u65b0\u9a8c\u8bc1\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\uff0c\u5efa\u8bae\u6309\u9700\u4f7f\u7528\u3002<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u629b\u51fa\u9519\u8bef\u4f1a\u89e6\u53d1\u6700\u8fd1\u7684\u9519\u8bef\u8fb9\u754c\uff0c\u5efa\u8bae\u8fd4\u56de\u7ed3\u6784\u5316\u7ed3\u679c\u800c\u975e\u76f4\u63a5\u629b\u51fa\u3002<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>React Server Actions \u6781\u5927\u5730\u7b80\u5316\u4e86\u5168\u6808 React \u5e94\u7528\u7684\u6570\u636e\u53d8\u66f4\u903b\u8f91\uff0c\u901a\u8fc7\u672c\u6587\u7684\u5165\u95e8\u4e0e\u8fdb\u9636\u793a\u4f8b\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86\u4ece\u8868\u5355\u63d0\u4ea4\u5230\u4e50\u89c2\u66f4\u65b0\u7684\u5b8c\u6574\u6d41\u7a0b\u3002\u8bb0\u4f4f\uff1a\u59cb\u7ec8\u9a8c\u8bc1\u8f93\u5165\u3001\u68c0\u67e5\u8ba4\u8bc1\u3001\u5408\u7406\u4f7f\u7528\u91cd\u65b0\u9a8c\u8bc1\uff0c\u624d\u80fd\u6784\u5efa\u51fa\u65e2\u9ad8\u6548\u53c8\u5b89\u5168\u7684\u5e94\u7528\u3002<\/p>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u63a2\u7d22 Server Actions \u4e0e Server Components \u7684\u6df1\u5ea6\u96c6\u6210\u3001\u4f7f\u7528 <code>useActionState<\/code> \u7ba1\u7406\u590d\u6742\u8868\u5355\u72b6\u6001\uff0c\u4ee5\u53ca\u7ed3\u5408\u6570\u636e\u5e93 ORM \u5b9e\u73b0\u5b8c\u6574 CRUD\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u6d45\u51fa\u5730\u4ecb\u7ecd React Server Actions\uff0c\u4ece\u57fa\u7840\u6982\u5ff5\u5230\u8fdb\u9636\u7528\u6cd5\uff0c\u5305\u542b\u8868\u5355\u63d0\u4ea4\u3001\u6570\u636e\u9a8c\u8bc1\u3001\u4e50\u89c2\u66f4\u65b0\u7b49\u5b9e\u6218\u4ee3\u7801\uff0c\u5e76\u5206\u4eab\u6027\u80fd\u4f18\u5316\u4e0e\u5b89\u5168\u6700\u4f73\u5b9e\u8df5\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,220,36],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-next-js","tag-react","tag-server-actions","tag-220","tag-36"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/249","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=249"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/249\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}