0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · درس

واجهة المستخدم التفاؤلية وحالات الانتظار مع Server Actions

ابنِ واجهات سريعة الاستجابة عبر الجمع بين Server Actions وuseOptimistic وuseFormStatus وuseTransition لتوفير ملاحظات فورية.

واجهة المستخدم التفاؤلية وحالات الانتظار مع Server Actions درس مجاني في Next.js 15 Fullstack (App Router + Server Actions) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.

الأسئلة الشائعة

هل درس «واجهة المستخدم التفاؤلية وحالات الانتظار مع Server Actions» مجاني؟

نعم — نص درس «واجهة المستخدم التفاؤلية وحالات الانتظار مع Server Actions» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Next.js 15 Fullstack (App Router + Server Actions)، انتقل إلى CoddyKit PRO. تتضمن دورة Next.js 15 Fullstack (App Router + Server Actions) 4 دروس في المجموع.

ماذا ستتعلم في «واجهة المستخدم التفاؤلية وحالات الانتظار مع Server Actions»؟

ابنِ واجهات سريعة الاستجابة عبر الجمع بين Server Actions وuseOptimistic وuseFormStatus وuseTransition لتوفير ملاحظات فورية. تتمرن على Next.js 15 Fullstack (App Router + Server Actions) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Next.js 15 Fullstack (App Router + Server Actions)؟

لا تُشترط خبرة سابقة. Next.js 15 Fullstack (App Router + Server Actions) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «واجهة المستخدم التفاؤلية وحالات الانتظار مع Server Actions»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Next.js 15 Fullstack (App Router + Server Actions) هذا؟

نعم. كل درس في Next.js 15 Fullstack (App Router + Server Actions) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. نماذج Server Actions الأساسية
  2. تغيير البيانات وإعادة التحقق
  3. معالجة الأخطاء في Actions
  4. واجهة المستخدم التفاؤلية وحالات الانتظار مع Server Actions
← العودة إلى Next.js 15 Fullstack (App Router + Server Actions)