Client-triggered mutations
Expose a server-side mutation such as adding an item to a cart while keeping database access on the server.
React 19+ · Directive
'use server' marks an async server-side function as callable from client-side code. Client calls become network requests, with serializable arguments and results handled by the framework.
'use server' marks an async server-side function as callable from client-side code. A framework turns it into a server reference; calling that reference sends serializable arguments over the network and returns a serialized result.
'use server'| Name | Type | Description |
|---|---|---|
function arguments | Serializable values | Arguments passed to the async Server Function; they must use types supported by React serialization. |
A serialized function result delivered to the client; the function itself must return a Promise because it is async.
// actions.ts
'use server';
export async function addToCart(productId: string, quantity: number) {
// Validate and authorize before mutating data.
return {productId, quantity};
}
// CartButton.tsx
'use client';
import {addToCart} from './actions';
export function CartButton() {
return <button onClick={() => addToCart('p-1', 1)}>Add</button>;
}The module directive makes the exported async function callable from client code through the framework's server-function mechanism.
Expose a server-side mutation such as adding an item to a cart while keeping database access on the server.
Place the directive at module scope so several exported async functions can be imported by Client Components.
Use a function as a form action or formAction so React invokes it in a Transition.
'use server' do?It marks an async server-side function as callable by client code. The framework creates a reference, and invoking that reference sends a request to the server and returns the serialized result.
Follow-up: Can a synchronous function use it? No. The documentation says the directive can only be used on async functions because the underlying call is a network operation.
A module-level directive marks every export in that file as a Server Function, allowing those exports to be imported by client code. A function-level directive marks only that function.
Follow-up: Why is module scope needed for client imports? The documentation requires the directive at module level when Server Functions are imported from client code.
Arguments cross a network boundary, so they must use React's supported serializable types. Server Functions should also be treated as public entry points: validate untrusted arguments and authorize mutations.
Follow-up: How should UI code invoke one? Prefer a Transition; functions supplied to a form's action or formAction are automatically invoked in a Transition.
Server Functionsuse clientServer ComponentsTransitionsform actionformAction