Skip to content

Commit 1b0bb73

Browse files
authored
Merge pull request #2 from ianalloway/devin/1771203328-more-docs-improvements
docs: Add troubleshooting section to 'use server' reference
2 parents cb47fcc + 2225f10 commit 1b0bb73

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

src/content/reference/rsc/use-server.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,97 @@ export default async function incrementLike() {
215215
```
216216

217217
To read a Server Function return value, you'll need to `await` the promise returned.
218+
219+
## Troubleshooting {/*troubleshooting*/}
220+
221+
### "Server Functions cannot be called during render" error {/*server-functions-cannot-be-called-during-render*/}
222+
223+
Server Functions should only be called in response to user actions (like form submissions or button clicks), not during component rendering.
224+
225+
```js
226+
// This will error - calling during render
227+
function BadComponent() {
228+
const data = await serverFunction(); // Don't do this!
229+
return <div>{data}</div>;
230+
}
231+
232+
// Correct - call in response to user action
233+
function GoodComponent() {
234+
const [data, setData] = useState(null);
235+
236+
const handleClick = async () => {
237+
const result = await serverFunction();
238+
setData(result);
239+
};
240+
241+
return <button onClick={handleClick}>Load Data</button>;
242+
}
243+
```
244+
245+
For data fetching during render, use a Server Component instead of a Server Function.
246+
247+
### "Arguments must be serializable" error {/*arguments-must-be-serializable*/}
248+
249+
Server Functions can only receive serializable arguments. You cannot pass functions, class instances, or React elements.
250+
251+
```js
252+
// This will error - functions aren't serializable
253+
async function saveData(callback) {
254+
'use server';
255+
// ...
256+
}
257+
258+
// Correct - pass serializable data
259+
async function saveData(userId, formData) {
260+
'use server';
261+
// ...
262+
}
263+
```
264+
265+
### Server Function not updating the UI {/*server-function-not-updating-ui*/}
266+
267+
If your Server Function runs but the UI doesn't update, make sure you're calling it inside a Transition when outside a form:
268+
269+
```js
270+
import { useTransition } from 'react';
271+
272+
function MyComponent() {
273+
const [isPending, startTransition] = useTransition();
274+
275+
const handleClick = () => {
276+
// Wrap in startTransition for proper UI updates
277+
startTransition(async () => {
278+
await myServerFunction();
279+
});
280+
};
281+
282+
return <button onClick={handleClick} disabled={isPending}>Submit</button>;
283+
}
284+
```
285+
286+
### "use server" directive not working {/*use-server-directive-not-working*/}
287+
288+
The `'use server'` directive must be:
289+
1. At the very beginning of the function body (not before the function)
290+
2. Written with single or double quotes (not backticks)
291+
3. Used only in async functions
292+
293+
```js
294+
// Wrong - directive before function
295+
'use server';
296+
async function myFunction() {
297+
// ...
298+
}
299+
300+
// Correct - directive inside function body
301+
async function myFunction() {
302+
'use server';
303+
// ...
304+
}
305+
306+
// Also correct - at top of file to mark all exports
307+
'use server';
308+
309+
export async function functionA() { /* ... */ }
310+
export async function functionB() { /* ... */ }
311+
```

0 commit comments

Comments
 (0)