Sub topic 4
Defining Server Functions inline and in a "use server" file
This chapter assumes you know React components, props and hooks, async/await, and basic HTML forms with FormData. It also assumes you know the "use server" directive marks code that runs on the server. Here you will see the two places you can write that directive and when to pick each.
Two ways to define a Server Function
A Server Function is an async function that runs on the server. You can define one in two ways:
- In a "use server" file. The directive goes at the top of the file and covers every export.
- Inline in a Server Component. The directive goes on the first line of one async function body and covers only that function.
In a "use server" file
Put 'use server' at the very top of the file, before any imports. Every exported function in that file becomes a Server Function.
// app/actions.js
'use server';
import { db } from './db';
export async function createNote(formData) {
const title = formData.get('title');
await db.notes.insert({ title });
}Here db is a placeholder for your data layer. Client Components can import createNote from this file. The bundler replaces the import with a reference, so the function body never ships to the browser.
Two rules apply to these files:
- Every export must be an async function. Exporting a constant, a class or a plain synchronous function causes an error.
- The directive must come before all other statements. If it comes after an import, it is treated as an ordinary string and does nothing.
Inline in a Server Component
For a function used by one Server Component, put 'use server' on the first line inside the function.
// 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>
);
}The function sits next to the form that uses it, which keeps small features easy to read. Only the function with the directive becomes a Server Function. The rest of Page stays an ordinary Server Component.
Choosing between them
| Question | Inline | "use server" file |
|---|---|---|
| Can a Client Component import it? | No | Yes |
| Can several components share it? | Awkward | Yes |
| Where does the directive go? | First line of the function body | Top of the file |
| Which functions does it cover? | That one function | Every export |
If a Client Component needs the function, use a file. Inline definitions are only available inside Server Components.
Common errors
- Inline directive in a Client Component. A file that starts with
'use client'cannot contain an inline'use server'function. Move the function to a separate "use server" file and import it. - Confusing the directives.
'use server'does not mark a Server Component. Server Components need no directive. It marks functions the client can call.'use client'marks the client boundary. - Non-async function. A Server Function must be async, because the client reaches it over the network.
- Treating a file as private. Every export in a "use server" file is callable from outside. Do not export a helper just so another server module can import it. Keep helpers in a file without the directive.
Inline functions can also read variables from the scope around them. Chapter 12 covers that behavior and bind.
Exercise
A Client Component with 'use client' at the top needs to call a function that inserts a note. You write the function in that same file and put 'use server' on its first line. Why does this fail, and how do you fix it?
Show answer
Inline "use server" functions are only allowed in Server Components, not in files marked 'use client'. Move the function into a separate file that starts with 'use server' (for example app/actions.js), export it as an async function, and import it into the Client Component.