Sub topic 5
Calling Server Functions from Client Components
This chapter assumes you know React hooks, async/await, and the difference between Server Components and Client Components. It also assumes you know how a "use server" file marks Server Functions. Here you will see what a Client Component receives when it imports a Server Function, and how to call one correctly.
What the client actually imports
A Client Component cannot define a Server Function inline. The 'use server' directive inside a function body only works in a Server Component. To use a Server Function from a Client Component, put it in a file that starts with 'use server' and import it.
// app/actions.js
'use server';
import { db } from './db';
export async function deleteNote(noteId) {
await db.notes.delete(noteId);
}The client bundle does not contain this code. The bundler replaces the import with a reference, which is an identifier for the function on the server. When the client calls the reference, React sends a request to the server. The server runs the real function and sends the result back.
From the caller's side, the reference looks like a normal async function. It always returns a Promise, even if the server code is not written as async-looking logic, because the call crosses the network.
Calling from an event handler
Outside of a form, call the function inside a transition. A transition tells React that the update is not urgent, and it lets you read a pending flag.
// app/DeleteButton.js
'use client';
import { useTransition } from 'react';
import { deleteNote } from './actions';
export function DeleteButton({ noteId }) {
const [isPending, startTransition] = useTransition();
return (
<button
disabled={isPending}
onClick={() => {
startTransition(async () => {
await deleteNote(noteId);
});
}}
>
{isPending ? 'Deleting…' : 'Delete'}
</button>
);
}isPending stays true until the async function finishes. Any state updates you make after an await inside the transition also need to be wrapped in another startTransition, because React loses track of the transition context after the first await.
Calling through a form
A form is the other common way to call a Server Function. Wrapping the function in useActionState gives you the latest returned state and a pending flag. The Server Function then receives the previous state first and the FormData second.
// app/NoteForm.js
'use client';
import { useActionState } from 'react';
import { createNote } from './actions';
export function NoteForm() {
const [state, formAction, isPending] = useActionState(createNote, { error: null });
return (
<form action={formAction}>
<input name="title" />
<button type="submit" disabled={isPending}>Add note</button>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}Common errors
- Defining the function inside a Client Component file. The build fails or the directive is ignored. Move the function into a
'use server'file and import it. - Calling it from a handler without a transition. The call still runs, but you get no pending state, and React does not coordinate the resulting update.
- Passing values that cannot be serialized. Functions, class instances, and DOM nodes cannot cross the network. Pass plain data such as strings, numbers, and objects of plain data.
- Assuming calls run in parallel. Server Function calls from the client are dispatched one at a time. If you need parallel reads, use a data-fetching approach instead.
Exercise
A Client Component calls await deleteNote(noteId) directly inside an onClick handler. The call works, but the button never shows a loading state. What change fixes this, and why?
Show answer
Wrap the call in startTransition(async () => { await deleteNote(noteId); }) from useTransition, and use the returned isPending flag to disable the button or change its label. The transition lets React track the async work and report when it is pending.