Skip to content

Commit 9925200

Browse files
author
Kartik Sahu
committed
docs: fix Server Functions code examples
Fixes #8537%0A%0AThis commit fixes multiple issues with the Server Functions examples:%0A%0A1. First example (Actions): Added return value {error: null} on success and fixed input to be controlled with value/onChange%0A%0A2. Second example (Form Actions): Renamed to updateNameForm and fixed to accept FormData parameter instead of string, matching React 19 form action behavior%0A%0A3. Third example (useActionState): Renamed to updateNameAction and fixed to accept (previousState, formData) parameters as required by useActionState%0A%0AAll examples now have correct function signatures matching their usage context.
1 parent c7d6b70 commit 9925200

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

‎src/content/reference/rsc/server-functions.md‎

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export async function updateName(name) {
109109
return {error: 'Name is required'};
110110
}
111111
await db.users.updateName(name);
112+
return {error: null};
112113
}
113114
```
114115

@@ -126,19 +127,17 @@ function UpdateName() {
126127
const submitAction = async () => {
127128
startTransition(async () => {
128129
const {error} = await updateName(name);
129-
startTransition(() => {
130-
if (error) {
131-
setError(error);
132-
} else {
133-
setName('');
134-
}
135-
});
130+
if (error) {
131+
setError(error);
132+
return;
133+
}
134+
setName('');
136135
})
137136
}
138137

139138
return (
140139
<form action={submitAction}>
141-
<input type="text" name="name" disabled={isPending}/>
140+
<input type="text" name="name" value={name} onChange={(e) => setName(e.target.value)} disabled={isPending}/>
142141
{error && <span>Failed: {error}</span>}
143142
</form>
144143
)
@@ -156,14 +155,27 @@ Server Functions work with the new Form features in React 19.
156155
You can pass a Server Function to a Form to automatically submit the form to the server:
157156

158157

159-
```js [[1, 3, "updateName"], [1, 7, "updateName"]]
158+
```js [[1, 3, "updateNameForm"], [1, 7, "updateNameForm"]]
159+
"use server";
160+
161+
export async function updateNameForm(formData) {
162+
const name = formData.get('name');
163+
if (!name) {
164+
return {error: 'Name is required'};
165+
}
166+
await db.users.updateName(name);
167+
return {error: null};
168+
}
169+
```
170+
171+
```js [[1, 3, "updateNameForm"], [1, 7, "updateNameForm"]]
160172
"use client";
161173

162-
import {updateName} from './actions';
174+
import {updateNameForm} from './actions';
163175

164176
function UpdateName() {
165177
return (
166-
<form action={updateName}>
178+
<form action={updateNameForm}>
167179
<input type="text" name="name" />
168180
</form>
169181
)
@@ -178,13 +190,26 @@ For more, see the docs for [Server Functions in Forms](/reference/rsc/use-server
178190

179191
You can call Server Functions with `useActionState` for the common case where you just need access to the action pending state and last returned response:
180192

181-
```js [[1, 3, "updateName"], [1, 6, "updateName"], [2, 6, "submitAction"], [2, 9, "submitAction"]]
193+
```js [[1, 3, "updateNameAction"], [1, 6, "updateNameAction"], [2, 6, "submitAction"], [2, 9, "submitAction"]]
194+
"use server";
195+
196+
export async function updateNameAction(previousState, formData) {
197+
const name = formData.get('name');
198+
if (!name) {
199+
return {error: 'Name is required'};
200+
}
201+
await db.users.updateName(name);
202+
return {error: null};
203+
}
204+
```
205+
206+
```js [[1, 3, "updateNameAction"], [1, 6, "updateNameAction"], [2, 6, "submitAction"], [2, 9, "submitAction"]]
182207
"use client";
183208

184-
import {updateName} from './actions';
209+
import {updateNameAction} from './actions';
185210

186211
function UpdateName() {
187-
const [state, submitAction, isPending] = useActionState(updateName, {error: null});
212+
const [state, submitAction, isPending] = useActionState(updateNameAction, {error: null});
188213

189214
return (
190215
<form action={submitAction}>

0 commit comments

Comments
 (0)