
هل 'use server' بتعمل Server Component؟ لأ، دي Server Functions
- كاتب موثّق
- React 19
محتويات المقال
'use client' في أول الملفات التفاعلية، فعشان يخلّي الصفحة تشتغل على الـ server حط 'use server' في أول OrdersPage.tsx. والملف كمان بيعمل export لـ cancelOrder، helper صغير الصفحة بتدّيه للـ list بتاعة الطلبات. الـ diff شكله بريء، واتعمله approve. وبعدها مراجعة security مسكته: الـ directive حوّل كل export لـ function المتصفح يقدر يناديها، ومنهم اللي بتمسح أي order id يوصلها.
الفكرة الغلط الشائعة
'use server' هو الطريقة اللي بتعلّم بيها Server Component، يعني عكس 'use client' بالظبط. حطه في أول الملف وكل اللي فيه هيفضل على الـ server.
'use server' فعلًا بيرسم خط بين كود الـ server وكود الـ client. بس ده خط تاني، مرسوم حوالين functions الـ client مسموحله يناديها، مش حوالين components بيعملها الـ server render.
اللي بيحصل فعلًا
الـ Server Components ملهاش directive
'use client' أو جاية عن طريقه. يعني إنك تخلّي صفحة Server Component مش محتاج أي كود خالص. انت دايمًا بتعلّم الناحية التانية بس: النقطة اللي الكود الـ client بيبدأ منها. والصفحة اللي في الأول كانت Server Component قبل ما حد يلمسها، والـ directive ماغيرش غير الحاجات اللي المتصفح مسموحله يناديها.
الـ 'use server' بيعلّم functions الـ client يقدر يناديها
'use server' بيتحط في أول جسم async function، أو في أول الملف. وفي الحالتين بيعمل . الـ framework بيدّي الـ client مرجع (reference) بدل الكود نفسه، ولما كود الـ client ينادي المرجع ده، React بيبعت network request فيه نسخة serialized من الـ arguments، ويشغّل الـ function على الـ server، ويرجّع النتيجة serialized. وعشان كده الـ directive بيشتغل على الـ async functions بس، والـ arguments لازم تبقى قيم ينفع تتعمل serialize زي الـ strings والـ objects العادية وFormData، عمرها ما تبقى JSX أو event objects.
"use server"; // meant: "this page is a Server Component"export default async function OrdersPage() { const user = await requireUser(); const orders = await findOrders(user.id); return <OrderList orders={orders} onCancel={cancelOrder} />;}// Now a Server Function too: any client, any orderIdexport async function cancelOrder(orderId: string) { await deleteOrder(orderId);}// A Server Component: no directive neededexport default async function OrdersPage() { const user = await requireUser(); const orders = await findOrders(user.id); async function cancelOrder(orderId: string) { "use server"; // only this function becomes callable const caller = await requireUser(); // check on every call await deleteOrderOwnedBy(caller.id, orderId); } return <OrderList orders={orders} onCancel={cancelOrder} />;}cancelOrder، بقوا Server Functions. أما في النسخة الصح فالصفحة مش محتاجة أي حاجة عشان تبقى Server Component، وcancelOrder بس هو اللي مكشوف. وكمان مابيثقش في اللي بيناديه: بيجيب اليوزر الحالي تاني على الـ server، ومابيمسحش الـ order غير لو اليوزر ده صاحبه.
إزاي الـ client بيناديها
<form action>، React بيناديه جوه لوحده، وده الـ pattern اللي في مقال الـ form actions في نفس المسار، ومع useActionState كمان بيعيد تشغيل الـ submits اللي حصلت قبل ما الـ hydration تخلص. وفي أي حتة تانية، الـ docs بتقول تناديه انت جوه Transition، وده بيدّيك pending state وoptimistic updates وتعامل مع الـ errors:
"use client";import { useTransition } from "react";type Props = { orderId: string; onCancel: (id: string) => Promise<void> };export function CancelButton({ orderId, onCancel }: Props) { const [isPending, startTransition] = useTransition(); return ( <button disabled={isPending} onClick={() => startTransition(() => onCancel(orderId))} > {isPending ? "Cancelling…" : "Cancel"} </button> );}'use server' بيخلّي الـ component يبقى Server Component | |
'use server' بيخلّي كود الملف سرّي على الـ server | |
تطبقها إزاي في مشروعك
اعمل list باللي ملفات 'use server' بتعمله export دوّر على 'use server'في أول الملفات. كل export هو endpoint ينفع يتنادى، فأي حاجة مش المفروض تبقى كده انقلها لـ module عادي.شيل الـ directive من الـ components الصفحات والـ components اللي المفروض تبقى Server Components مش محتاجة directive. لو واحد فيهم في أوله 'use server'، امسحه.احمي كل Server Function في أول كل واحدة، اعمل validation للـ arguments، واتأكد تاني على الـ server إن اليوزر الحالي مسموحله يعمل كده. طلّع القراية بره، وسيب الكتابة جوه حمّل الداتا في الـ Server Components، ونادي الـ Server Functions للـ mutations عن طريق <form action>أوuseActionStateأوstartTransition.
الخلاصة
مفيش directive للـ Server Components: هي الأساس، والـ 'use client'بيحدد الكود الـ client بيبدأ منين.الـ 'use server'بيعمل Server Functions: async functions الـ client بيناديها على الشبكة بـ arguments متعملها serialize.لو في أول الملف، بيكشف كل export، فاعتبر كل واحد endpoint مفتوح للكل: validation وauthorization. استخدم الـ Server Functions للـ mutations، من الفورمز أو الـ Transitions، واقرا الداتا في الـ Server Components.
التعليقات (0)