{"id":220,"date":"2026-07-30T09:25:13","date_gmt":"2026-07-30T01:25:13","guid":{"rendered":"https:\/\/yykcj.com\/?p=220"},"modified":"2026-07-30T09:25:13","modified_gmt":"2026-07-30T01:25:13","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%e5%a4%84%e7%90%86%e5%88%b0%e6%95%b0%e6%8d%ae%e7%aa%81%e5%8f%98%e7%9a%84%e6%9c%80%e4%bd%b3-2","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=220","title":{"rendered":"React Server Actions \u5165\u95e8\u4e0e\u8fdb\u9636\uff1a\u4ece\u8868\u5355\u5904\u7406\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\u4e2a\u91cd\u78c5\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u76f4\u63a5\u8c03\u7528\u670d\u52a1\u7aef\u51fd\u6570\uff0c\u800c\u65e0\u9700\u624b\u52a8\u7f16\u5199 API \u8def\u7531\u3002\u8fd9\u6781\u5927\u7b80\u5316\u4e86\u6570\u636e\u7a81\u53d8\uff08Mutation\uff09\u7684\u6d41\u7a0b\uff0c\u5c24\u5176\u662f\u8868\u5355\u63d0\u4ea4\u3001\u6570\u636e\u66f4\u65b0\u7b49\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u5bf9 Server Actions \u7684\u7406\u89e3\u505c\u7559\u5728\u8868\u9762\uff0c\u5bb9\u6613\u9677\u5165\u201c\u8fc7\u5ea6\u4f7f\u7528\u201d\u6216\u201c\u9519\u8bef\u4f7f\u7528\u201d\u7684\u9677\u9631\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u96f6\u5f00\u59cb\uff0c\u6df1\u5165\u7406\u89e3 Server Actions \u7684\u539f\u7406\u3001\u4f7f\u7528\u573a\u666f\u4ee5\u53ca\u8fdb\u9636\u6280\u5de7\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f Server Actions\uff1f<\/h2>\n<p>\u7b80\u5355\u6765\u8bf4\uff0cServer Actions \u662f\u5b9a\u4e49\u5728\u670d\u52a1\u7aef\u7684\u5f02\u6b65\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u901a\u8fc7 <code>\"use server\"<\/code> \u6307\u4ee4\u8c03\u7528\u3002\u5b83\u4eec\u76f4\u63a5\u8fd0\u884c\u5728\u670d\u52a1\u7aef\uff0c\u53ef\u4ee5\u5b89\u5168\u5730\u8bbf\u95ee\u6570\u636e\u5e93\u3001\u6587\u4ef6\u7cfb\u7edf\u7b49\u8d44\u6e90\uff0c\u800c\u65e0\u9700\u66b4\u9732 API \u7aef\u70b9\u3002<\/p>\n<h3>\u57fa\u672c\u7528\u6cd5<\/h3>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a Server Action \u6587\u4ef6\uff08\u4f8b\u5982 <code>app\/actions.ts<\/code>\uff09\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ app\/actions.ts\n&quot;use server&quot;;\n\nimport { revalidatePath } from &quot;next\/cache&quot;;\n\nexport async function createUser(formData: FormData) {\n  const name = formData.get(&quot;name&quot;);\n  const email = formData.get(&quot;email&quot;);\n  \n  \/\/ \u6a21\u62df\u6570\u636e\u5e93\u64cd\u4f5c\n  console.log(`Creating user: ${name}, ${email}`);\n  \n  \/\/ \u91cd\u65b0\u9a8c\u8bc1\u8def\u5f84\u4ee5\u5237\u65b0\u6570\u636e\n  revalidatePath(&quot;\/users&quot;);\n  \n  return { success: true };\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u5b83\uff1a<\/p>\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;Create User&lt;\/button&gt;\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u8868\u5355\u7684 <code>action<\/code> \u5c5e\u6027\u53ef\u4ee5\u76f4\u63a5\u63a5\u6536 Server Action \u51fd\u6570\uff0cReact \u4f1a\u81ea\u52a8\u5904\u7406\u63d0\u4ea4\u5e76\u663e\u793a\u52a0\u8f7d\u72b6\u6001\u3002<\/p>\n<h2>\u8fdb\u9636\u7528\u6cd5\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<h3>1. \u5904\u7406\u52a0\u8f7d\u72b6\u6001\u4e0e\u9519\u8bef<\/h3>\n<p>Server Actions \u672c\u8eab\u4e0d\u63d0\u4f9b\u52a0\u8f7d\u72b6\u6001\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>useFormStatus<\/code> \u548c <code>useFormState<\/code> \u94a9\u5b50\u6765\u589e\u5f3a\u4f53\u9a8c\u3002<\/p>\n<pre><code class=\"language-tsx\">\n&quot;use client&quot;;\n\nimport { useFormStatus } from &quot;react-dom&quot;;\nimport { createUser } from &quot;.\/actions&quot;;\n\nfunction SubmitButton() {\n  const { pending } = useFormStatus();\n  return (\n    &lt;button type=&quot;submit&quot; disabled={pending}&gt;\n      {pending ? &quot;Creating...&quot; : &quot;Create User&quot;}\n    &lt;\/button&gt;\n  );\n}\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;SubmitButton \/&gt;\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u9519\u8bef\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>useFormState<\/code>\uff1a<\/p>\n<pre><code class=\"language-tsx\">\n&quot;use client&quot;;\n\nimport { useFormState } from &quot;react-dom&quot;;\nimport { createUser } from &quot;.\/actions&quot;;\n\nconst initialState = { message: null };\n\nfunction Form() {\n  const [state, formAction] = useFormState(createUser, initialState);\n  \n  return (\n    &lt;form action={formAction}&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;Create User&lt;\/button&gt;\n      {state?.message &amp;&amp; &lt;p&gt;{state.message}&lt;\/p&gt;}\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<p>\u5bf9\u5e94\u7684 Server Action \u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u72b6\u6001\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n&quot;use server&quot;;\n\nexport async function createUser(prevState: any, formData: FormData) {\n  try {\n    \/\/ \u6a21\u62df\u6570\u636e\u5e93\u64cd\u4f5c\u53ef\u80fd\u5931\u8d25\n    const name = formData.get(&quot;name&quot;);\n    if (!name) throw new Error(&quot;Name is required&quot;);\n    \n    return { message: &quot;User created successfully&quot; };\n  } catch (error) {\n    return { message: error.message };\n  }\n}\n<\/code><\/pre>\n<h3>2. \u53c2\u6570\u9a8c\u8bc1\u4e0e\u7c7b\u578b\u5b89\u5168<\/h3>\n<p>Server Actions \u63a5\u6536\u7684\u53c2\u6570\u53ef\u4ee5\u662f FormData \u6216\u666e\u901a\u5bf9\u8c61\u3002\u63a8\u8350\u4f7f\u7528 <code>zod<\/code> \u8fdb\u884c\u9a8c\u8bc1\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n&quot;use server&quot;;\n\nimport { z } from &quot;zod&quot;;\n\nconst schema = z.object({\n  name: z.string().min(2),\n  email: z.string().email(),\n});\n\nexport async function createUser(prevState: any, formData: FormData) {\n  const validatedFields = schema.safeParse({\n    name: formData.get(&quot;name&quot;),\n    email: formData.get(&quot;email&quot;),\n  });\n  \n  if (!validatedFields.success) {\n    return {\n      errors: validatedFields.error.flatten().fieldErrors,\n    };\n  }\n  \n  \/\/ \u5b89\u5168\u5730\u4f7f\u7528 validatedFields.data\n  console.log(validatedFields.data);\n  \n  return { message: &quot;Success&quot; };\n}\n<\/code><\/pre>\n<h3>3. \u4e0e\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210\uff08\u5982 Prisma\uff09<\/h3>\n<p>Server Actions \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 Prisma\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n&quot;use server&quot;;\n\nimport { prisma } from &quot;@\/lib\/prisma&quot;;\nimport { revalidatePath } from &quot;next\/cache&quot;;\n\nexport async function addTodo(formData: FormData) {\n  const title = formData.get(&quot;title&quot;) as string;\n  \n  await prisma.todo.create({\n    data: { title },\n  });\n  \n  revalidatePath(&quot;\/todos&quot;);\n}\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u786e\u4fdd Prisma \u5ba2\u6237\u7aef\u5728\u670d\u52a1\u7aef\u521d\u59cb\u5316\uff0c\u4e0d\u8981\u5728\u5ba2\u6237\u7aef\u5bfc\u5165\u3002<\/p>\n<h3>4. \u6e10\u8fdb\u589e\u5f3a\u4e0e\u4e50\u89c2\u66f4\u65b0<\/h3>\n<p>Server Actions \u5929\u7136\u652f\u6301\u6e10\u8fdb\u589e\u5f3a\uff08\u5373\u4f7f JavaScript \u672a\u52a0\u8f7d\uff0c\u8868\u5355\u4e5f\u80fd\u63d0\u4ea4\uff09\u3002\u5bf9\u4e8e\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e50\u89c2\u66f4\u65b0\uff1a<\/p>\n<pre><code class=\"language-tsx\">\n&quot;use client&quot;;\n\nimport { useOptimistic } from &quot;react&quot;;\nimport { addTodo } from &quot;.\/actions&quot;;\n\nexport default function TodoList({ todos }) {\n  const [optimisticTodos, addOptimisticTodo] = useOptimistic(\n    todos,\n    (state, newTodo) =&gt; [...state, newTodo]\n  );\n  \n  async function formAction(formData) {\n    const title = formData.get(&quot;title&quot;);\n    addOptimisticTodo({ id: Date.now(), title, completed: false });\n    await addTodo(formData);\n  }\n  \n  return (\n    &lt;form action={formAction}&gt;\n      &lt;input name=&quot;title&quot; required \/&gt;\n      &lt;button type=&quot;submit&quot;&gt;Add&lt;\/button&gt;\n      &lt;ul&gt;\n        {optimisticTodos.map(todo =&gt; (\n          &lt;li key={todo.id}&gt;{todo.title}&lt;\/li&gt;\n        ))}\n      &lt;\/ul&gt;\n    &lt;\/form&gt;\n  );\n}\n<\/code><\/pre>\n<h3>5. \u907f\u514d\u5e38\u89c1\u9677\u9631<\/h3>\n<ul>\n<li><strong>\u4e0d\u8981\u5728\u5ba2\u6237\u7aef\u7ec4\u4ef6\u4e2d\u5bfc\u5165\u670d\u52a1\u7aef\u6a21\u5757<\/strong>\uff1aServer Actions \u6587\u4ef6\u5e94\u53ea\u5305\u542b <code>\"use server\"<\/code> \u51fd\u6570\uff0c\u4e0d\u8981\u5bfc\u5165\u6570\u636e\u5e93\u5ba2\u6237\u7aef\u7b49\u3002<\/li>\n<li><strong>\u6ce8\u610f\u5b89\u5168<\/strong>\uff1aServer Actions \u662f\u516c\u5f00\u7684\uff0c\u4e0d\u8981\u4fe1\u4efb\u7528\u6237\u8f93\u5165\uff0c\u59cb\u7ec8\u8fdb\u884c\u9a8c\u8bc1\u3002<\/li>\n<li><strong>\u4e0d\u8981\u6ee5\u7528<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\u83b7\u53d6\uff0c\u4ecd\u7136\u63a8\u8350\u4f7f\u7528\u670d\u52a1\u7aef\u7ec4\u4ef6\u6216 API \u8def\u7531\u3002Server Actions \u4e3b\u8981\u7528\u4e8e\u7a81\u53d8\u3002<\/li>\n<li><strong>\u7f13\u5b58\u5931\u6548<\/strong>\uff1a\u4f7f\u7528 <code>revalidatePath<\/code> \u6216 <code>revalidateTag<\/code> \u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u3002<\/li>\n<\/ul>\n<h2>\u5b9e\u6218\uff1a\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u8bc4\u8bba\u7cfb\u7edf<\/h2>\n<p>\u7ed3\u5408\u4e0a\u8ff0\u77e5\u8bc6\uff0c\u6211\u4eec\u6765\u6784\u5efa\u4e00\u4e2a\u5e26\u4e50\u89c2\u66f4\u65b0\u7684\u8bc4\u8bba\u8868\u5355\u3002<\/p>\n<h3>1. \u5b9a\u4e49 Server Action<\/h3>\n<pre><code class=\"language-typescript\">\n\/\/ app\/actions.ts\n&quot;use server&quot;;\n\nimport { z } from &quot;zod&quot;;\nimport { revalidatePath } from &quot;next\/cache&quot;;\n\nconst commentSchema = z.object({\n  postId: z.string(),\n  content: z.string().min(1).max(500),\n});\n\nexport async function addComment(prevState: any, formData: FormData) {\n  const validated = commentSchema.safeParse({\n    postId: formData.get(&quot;postId&quot;),\n    content: formData.get(&quot;content&quot;),\n  });\n  \n  if (!validated.success) {\n    return { errors: validated.error.flatten().fieldErrors };\n  }\n  \n  \/\/ \u6a21\u62df\u6570\u636e\u5e93\u64cd\u4f5c\n  const comment = {\n    id: Date.now(),\n    postId: validated.data.postId,\n    content: validated.data.content,\n    createdAt: new Date().toISOString(),\n  };\n  \n  console.log(&quot;Comment added:&quot;, comment);\n  \n  revalidatePath(`\/posts\/${validated.data.postId}`);\n  \n  return { comment };\n}\n<\/code><\/pre>\n<h3>2. \u5ba2\u6237\u7aef\u7ec4\u4ef6<\/h3>\n<pre><code class=\"language-tsx\">\n\/\/ app\/components\/CommentForm.tsx\n&quot;use client&quot;;\n\nimport { useFormState, useOptimistic } from &quot;react-dom&quot;;\nimport { addComment } from &quot;..\/actions&quot;;\n\nconst initialState = { errors: null, comment: null };\n\nexport default function CommentForm({ postId }) {\n  const [state, formAction] = useFormState(addComment, initialState);\n  const [optimisticComments, setOptimisticComments] = useOptimistic([], (state, newComment) =&gt; [...state, newComment]);\n  \n  async function handleSubmit(formData) {\n    const newComment = {\n      id: Date.now(),\n      postId,\n      content: formData.get(&quot;content&quot;),\n      createdAt: new Date().toISOString(),\n    };\n    setOptimisticComments(newComment);\n    await formAction(formData);\n  }\n  \n  return (\n    &lt;div&gt;\n      &lt;form action={handleSubmit}&gt;\n        &lt;input type=&quot;hidden&quot; name=&quot;postId&quot; value={postId} \/&gt;\n        &lt;textarea name=&quot;content&quot; placeholder=&quot;Write a comment...&quot; required \/&gt;\n        &lt;button type=&quot;submit&quot;&gt;Submit&lt;\/button&gt;\n        {state?.errors?.content &amp;&amp; &lt;p style={{color: &#x27;red&#x27;}}&gt;{state.errors.content}&lt;\/p&gt;}\n      &lt;\/form&gt;\n      &lt;ul&gt;\n        {optimisticComments.map(comment =&gt; (\n          &lt;li key={comment.id}&gt;{comment.content} (optimistic)&lt;\/li&gt;\n        ))}\n      &lt;\/ul&gt;\n    &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>Server Actions \u662f React \u751f\u6001\u4e2d\u6570\u636e\u7a81\u53d8\u7684\u4e00\u628a\u5229\u5668\uff0c\u5b83\u7b80\u5316\u4e86\u524d\u540e\u7aef\u4ea4\u4e92\uff0c\u540c\u65f6\u4fdd\u6301\u4e86\u670d\u52a1\u7aef\u7684\u5b89\u5168\u6027\u548c\u6027\u80fd\u3002\u672c\u6587\u4ece\u57fa\u7840\u7528\u6cd5\u5230\u8fdb\u9636\u5b9e\u8df5\uff0c\u6db5\u76d6\u4e86\u8868\u5355\u5904\u7406\u3001\u9a8c\u8bc1\u3001\u4e50\u89c2\u66f4\u65b0\u7b49\u6838\u5fc3\u573a\u666f\u3002\u8bb0\u4f4f\uff0cServer Actions \u6700\u9002\u5408\u7528\u4e8e\u8868\u5355\u63d0\u4ea4\u548c\u6570\u636e\u66f4\u65b0\uff0c\u800c\u5bf9\u4e8e\u6570\u636e\u83b7\u53d6\uff0c\u670d\u52a1\u7aef\u7ec4\u4ef6\u4ecd\u7136\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u63a2\u7d22 Server Actions \u4e0e\u6d41\u5f0f\u4f20\u8f93\uff08Streaming\uff09\u7684\u7ed3\u5408\uff0c\u6216\u8005\u7814\u7a76\u5982\u4f55\u5bf9 Server Actions \u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7528\u597d\u8fd9\u4e2a\u7279\u6027\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/react.dev\/reference\/react\/use-server\">React Server Actions \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/nextjs.org\/docs\/app\/building-your-application\/data-fetching\/server-actions-and-mutations\">Next.js Server Actions \u6307\u5357<\/a><\/li>\n<li><a href=\"https:\/\/zod.dev\/\">Zod \u9a8c\u8bc1\u5e93<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u8bb2\u89e3 React Server Actions \u7684\u6838\u5fc3\u6982\u5ff5\u3001\u4f7f\u7528\u573a\u666f\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u901a\u8fc7\u591a\u4e2a\u5b9e\u6218\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u7528 Server Actions \u7b80\u5316\u670d\u52a1\u7aef\u6570\u636e\u7a81\u53d8\uff0c\u907f\u514d\u5e38\u89c1\u9677\u9631\uff0c\u63d0\u5347\u5e94\u7528\u6027\u80fd\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,36,66,98],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-next-js","tag-react","tag-server-actions","tag-36","tag-66","tag-98"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/220","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=220"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}