Next.js 15 Fullstack (App Router + Server Actions) · 课时

使用服务器操作实现乐观界面与待处理状态

将服务器操作与 useOptimistic、useFormStatus 和 useTransition 结合,构建响应迅速、即时反馈的界面。

第 4 / 4 课13 个步骤

使用服务器操作实现乐观界面与待处理状态 是 CoddyKit 上的免费 Next.js 15 Fullstack (App Router + Server Actions) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack (App Router + Server Actions) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Optimistic UI

Server Actions involve a network round trip. Optimistic UI updates the screen immediately as if the action succeeded, then reconciles with the real result for a fast feel.

Pending State with useFormStatus

The useFormStatus hook reads whether the parent form is submitting, letting you disable buttons or show spinners.

"use client";
import { useFormStatus } from "react-dom";
export function Submit() {
  const { pending } = useFormStatus();
  return <button disabled={pending}>{pending ? "Saving..." : "Save"}</button>;
}

useOptimistic Basics

The useOptimistic hook gives you a temporary optimistic state that overrides the real state until the action resolves.

"use client";
import { useOptimistic } from "react";
const [optimistic, addOptimistic] = useOptimistic(messages, (state, next) => [...state, next]);

Wiring an Optimistic Add

Call addOptimistic before invoking the action so the item appears instantly; the server then revalidates the true list.

async function action(formData) {
  addOptimistic({ text: formData.get("text"), sending: true });
  await sendMessage(formData);
}

useTransition

useTransition marks state updates as non-urgent and exposes isPending, useful when calling a Server Action outside a form.

"use client";
const [isPending, startTransition] = useTransition();
startTransition(() => { void likePost(id); });

Reconciliation

When the action finishes and the data revalidates, React discards the optimistic state and renders the authoritative server data automatically.

Handling Failures

If the action throws, the optimistic update is rolled back. Surface an error so the user knows their change did not persist.

Progressive Enhancement

Forms with Server Actions work even before JS loads. useFormStatus and optimistic hooks layer enhancement on top for interactive clients.

Disabling Double Submits

Use the pending flag to prevent duplicate submissions, which would otherwise fire the Server Action twice.

Keeping Inputs Responsive

Clear or reset the input immediately on optimistic add so the user can keep typing without waiting for the server.

Choosing the Right Hook

Use useFormStatus inside forms for pending UI, useOptimistic for instant list/state updates, and useTransition for non-form action calls.

Quick Check

What does useOptimistic provide?

Recap

You combined Server Actions with useFormStatus for pending UI, useOptimistic for instant updates with automatic reconciliation, and useTransition for non-form calls — rolling back gracefully on failure for a snappy, resilient UX.

免费开始

用 AI 导师学习 TypeScript — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
22
课程
88

常见问题解答

「使用服务器操作实现乐观界面与待处理状态」课时是免费的吗?

是的 — 「使用服务器操作实现乐观界面与待处理状态」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack (App Router + Server Actions) 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。

「使用服务器操作实现乐观界面与待处理状态」这节课中我会学到什么?

将服务器操作与 useOptimistic、useFormStatus 和 useTransition 结合,构建响应迅速、即时反馈的界面。 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack (App Router + Server Actions),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Next.js 15 Fullstack (App Router + Server Actions) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack (App Router + Server Actions) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用服务器操作实现乐观界面与待处理状态」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Next.js 15 Fullstack (App Router + Server Actions) 课中编写并运行代码吗?

能。每节 Next.js 15 Fullstack (App Router + Server Actions) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 基础服务器操作表单
  2. 修改数据与重新验证
  3. 操作中的错误处理
  4. 使用服务器操作实现乐观界面与待处理状态
← 返回 Next.js 15 Fullstack (App Router + Server Actions)