Sub topic 2
What "use server" marks (and what it does not)
This chapter assumes you know React components, props and hooks, async/await, and the basics of HTML forms and FormData. It also assumes you know that a browser talks to a server over HTTP requests.
Two directives, two different jobs
React has two directives that look like opposites: "use client" and "use server". They are not opposites.
"use client"marks a module as the start of the client bundle. That module and everything it imports run in the browser."use server"marks functions that run on the server but that client code is allowed to call.
Server Components need no directive. Components run on the server by default in a framework that supports them. So "use server" does not mean "this is a Server Component."
| Directive | Marks | Effect |
|---|---|---|
| none | A component module | Runs on the server as a Server Component |
| `"use client"` | A module | Starts the client bundle for that module and its imports |
| `"use server"` | An async function, or all exports of a file | Creates a callable server entry point |
What the directive does
When you mark a function with "use server", the bundler turns it into a Server Function. Client code that imports it does not receive the function body. It receives a reference, which is an identifier for the function. Calling the reference sends a request to the server. The server runs the real function and sends back the result.
Where you can place the directive
You can place the directive in two positions.
At the top of a file, it marks every export as a Server Function. The directive must come before any imports.
// app/actions.js
'use server';
import { db } from './db';
export async function createNote(formData) {
const title = formData.get('title');
await db.notes.insert({ title });
}At the top of a function body, it marks that one function. This works only inside a Server Component, so the function can be defined next to the form that uses it.
// app/page.js (a Server Component)
import { db } from './db';
export default function Page() {
async function createNote(formData) {
'use server';
await db.notes.insert({ title: formData.get('title') });
}
return (
<form action={createNote}>
<input name="title" required />
<button type="submit">Add note</button>
</form>
);
}A Client Component cannot define a Server Function inline. It must import one from a "use server" file.
What the directive does not do
These are the usual misunderstandings.
- It does not make code server-only. A module without
"use server"that reads secrets can still be imported by client code by mistake. The directive controls what can be called remotely, not what can be imported. - It does not mark Server Components. Writing
"use server"at the top of a component file is a mistake. It would turn every export into a callable endpoint. - It does not make a function private. A Server Function is reachable by any client that can send the right request, not only by your own UI. A later chapter covers this in detail.
- It does not allow synchronous functions. In a
"use server"file, every export must be an async function, because the call crosses the network.
A common error
This file breaks because of an exported value that is not an async function:
// app/actions.js
'use server';
export const MAX_TITLE = 100; // Error: not an async function
export async function createNote(formData) {
// ...
}The fix is to move the constant to another module and import it where needed. Only async functions belong in the file.
Exercise
A teammate puts "use server" at the top of a file containing a page component, expecting it to render only on the server. What goes wrong, and what is the correct way to get server rendering?
Show answer
"use server" does not mark Server Components. It marks the file's exports as Server Functions, which become callable endpoints. A component does not need that, and exports that are not async functions cause an error. Remove the directive. Components render on the server by default, and "use client" is only needed to opt into the client.