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

Optimistische UI und ausstehende Zustände mit Server Actions

Erstellen Sie reaktionsschnelle Oberflächen, indem Sie Server Actions mit useOptimistic, useFormStatus und useTransition für sofortiges Feedback kombinieren

Lektion 4 von 413 Schritte

Optimistische UI und ausstehende Zustände mit Server Actions ist eine kostenlose Next.js 15 Fullstack (App Router + Server Actions)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Next.js 15 Fullstack (App Router + Server Actions)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Next.js 15 Fullstack (App Router + Server Actions)-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Kostenlos starten

Lerne TypeScript mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
22
Lektionen
88

Häufig gestellte Fragen

Ist die Lektion „Optimistische UI und ausstehende Zustände mit Server Actions“ kostenlos?

Ja — der vollständige Text von „Optimistische UI und ausstehende Zustände mit Server Actions“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Next.js 15 Fullstack (App Router + Server Actions)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Next.js 15 Fullstack (App Router + Server Actions)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Optimistische UI und ausstehende Zustände mit Server Actions“?

Erstellen Sie reaktionsschnelle Oberflächen, indem Sie Server Actions mit useOptimistic, useFormStatus und useTransition für sofortiges Feedback kombinieren Du übst Next.js 15 Fullstack (App Router + Server Actions) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Next.js 15 Fullstack (App Router + Server Actions) zu starten?

Keine Vorkenntnisse erforderlich. Next.js 15 Fullstack (App Router + Server Actions) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Optimistische UI und ausstehende Zustände mit Server Actions“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Next.js 15 Fullstack (App Router + Server Actions)-Lektion Code schreiben und ausführen?

Ja. Jede Next.js 15 Fullstack (App Router + Server Actions)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Grundlegende Formulare mit Server Actions
  2. Daten mutieren und revalidieren
  3. Fehlerbehandlung in Actions
  4. Optimistische UI und ausstehende Zustände mit Server Actions
← Zurück zu Next.js 15 Fullstack (App Router + Server Actions)