Skip to content

Commit 912781a

Browse files
committed
Polish Server Functions examples
1 parent 06a3347 commit 912781a

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ For more, see the docs for [`"use server"`](/reference/rsc/use-server).
7272

7373
Client Components can import Server Functions from files that use the `"use server"` directive:
7474

75-
```js [[1, 3, "createNote"]]
75+
```js [[1, 4, "createNote"]]
76+
// actions.js
7677
"use server";
7778

7879
export async function createNote() {
@@ -82,12 +83,15 @@ export async function createNote() {
8283

8384
When the bundler builds the `EmptyNote` Client Component, it will create a reference to the `createNote` function in the bundle. When the `button` is clicked, React will send a request to the server to execute the `createNote` function using the reference provided:
8485

85-
```js [[1, 3, "createNote"], [1, 6, "createNote"]]
86+
```js [[1, 4, "createNote"], [1, 7, "createNote"], [1, 9, "createNote"]]
87+
// EmptyNote.js
8688
"use client";
8789

8890
import {createNote} from './actions';
8991

9092
function EmptyNote() {
93+
console.log(createNote);
94+
// {$$typeof: Symbol.for("react.server.reference"), $$id: 'createNote'}
9195
return <button onClick={() => createNote()}>Create Empty Note</button>;
9296
}
9397
```
@@ -98,7 +102,8 @@ For more, see the docs for [`"use server"`](/reference/rsc/use-server).
98102

99103
Server Functions can be called from Actions on the client:
100104

101-
```js [[1, 3, "updateName"]]
105+
```js [[1, 4, "updateName"]]
106+
// actions.js
102107
"use server";
103108

104109
export async function updateName(name) {
@@ -110,7 +115,8 @@ export async function updateName(name) {
110115
}
111116
```
112117

113-
```js [[1, 4, "updateName"], [1, 14, "updateName"], [2, 12, "submitAction"], [2, 26, "submitAction"]]
118+
```js [[1, 5, "updateName"], [1, 15, "updateName"], [2, 13, "submitAction"], [2, 27, "submitAction"]]
119+
// UpdateName.js
114120
"use client";
115121

116122
import {useState, useTransition} from 'react';
@@ -125,7 +131,8 @@ function UpdateName() {
125131
function submitAction() {
126132
startTransition(async () => {
127133
const {error} = await updateName(name);
128-
// State updates after an await aren't automatically Transitions, so wrap them
134+
// State updates after await aren't automatically marked as Transitions,
135+
// so wrap them in another startTransition.
129136
startTransition(() => {
130137
setError(error);
131138
if (!error) {
@@ -154,13 +161,14 @@ This allows you to access the `isPending` state of the Server Function by wrappi
154161

155162
For more, see the docs for [Calling a Server Function outside of `<form>`](/reference/rsc/use-server#calling-a-server-function-outside-of-form).
156163

157-
### Using a Server Function as a Form Action {/*using-server-functions-with-form-actions*/}
164+
### Passing a Server Function to the `<form>` `action` prop {/*using-server-functions-with-form-actions*/}
158165

159166
Server Functions work with the new Form features in React 19.
160167

161-
You can pass a Server Function to a Form to automatically submit the form to the server. React passes the submitted `FormData` to the Server Function as its first argument:
168+
Pass a Server Function to the `<form>` `action` prop to submit the form to the server. React passes the submitted [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) to the Server Function as its first argument:
162169

163-
```js [[1, 3, "updateName"]]
170+
```js [[1, 4, "updateName"]]
171+
// actions.js
164172
"use server";
165173

166174
export async function updateName(formData) {
@@ -172,7 +180,8 @@ export async function updateName(formData) {
172180
}
173181
```
174182

175-
```js [[1, 3, "updateName"], [1, 7, "updateName"]]
183+
```js [[1, 4, "updateName"], [1, 8, "updateName"]]
184+
// UpdateName.js
176185
"use client";
177186

178187
import {updateName} from './actions';
@@ -186,15 +195,16 @@ function UpdateName() {
186195
}
187196
```
188197

189-
When the Form Action succeeds, React will automatically reset the form's uncontrolled fields. Server Function forms can be submitted before the JavaScript bundle loads. You can add `useActionState` to access the pending state and last response.
198+
When the Server Function passed to the `<form>` `action` prop succeeds, React automatically resets the form's uncontrolled fields. Users can submit the form before its JavaScript bundle loads. Use `useActionState` to access the Action's pending state and most recent return value.
190199

191200
For more, see the docs for [Server Functions in Forms](/reference/rsc/use-server#server-functions-in-forms).
192201

193202
### Calling a Server Function with `useActionState` {/*server-functions-with-use-action-state*/}
194203

195-
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. The Server Function receives the previous state as its first argument and the submitted `FormData` as its second argument. Its return value becomes the next state:
204+
Call a Server Function with `useActionState` to access the Action's pending state and most recent return value. The Server Function receives the previous state as its first argument and the submitted [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) as its second argument. Its return value becomes the next state:
196205

197-
```js [[1, 3, "updateName"]]
206+
```js [[1, 4, "updateName"]]
207+
// actions.js
198208
"use server";
199209

200210
export async function updateName(previousState, formData) {
@@ -207,7 +217,8 @@ export async function updateName(previousState, formData) {
207217
}
208218
```
209219

210-
```js [[1, 4, "updateName"], [1, 7, "updateName"], [2, 7, "submitAction"], [2, 10, "submitAction"]]
220+
```js [[1, 5, "updateName"], [1, 8, "updateName"], [2, 8, "submitAction"], [2, 11, "submitAction"]]
221+
// UpdateName.js
211222
"use client";
212223

213224
import {useActionState} from 'react';
@@ -225,15 +236,16 @@ function UpdateName() {
225236
}
226237
```
227238

228-
When using `useActionState` with a Server Function, the form can be submitted before hydration finishes and the Server Function's response can be shown before the app has hydrated.
239+
When the function passed to `useActionState` is a Server Function, users can submit the form before hydration finishes. React can display the Server Function's return value before JavaScript loads.
229240

230241
For more, see the docs for [`useActionState`](/reference/react/useActionState).
231242

232243
### Supporting progressive enhancement with `useActionState` {/*progressive-enhancement-with-useactionstate*/}
233244

234245
Server Functions also support progressive enhancement with the third argument of `useActionState`.
235246

236-
```js [[1, 4, "updateName"], [1, 7, "updateName"], [2, 7, "/name/update"], [3, 7, "submitAction"], [3, 10, "submitAction"]]
247+
```js [[1, 5, "updateName"], [1, 8, "updateName"], [2, 8, "/name/update"], [3, 8, "submitAction"], [3, 11, "submitAction"]]
248+
// UpdateName.js
237249
"use client";
238250

239251
import {useActionState} from 'react';
@@ -250,6 +262,6 @@ function UpdateName() {
250262
}
251263
```
252264

253-
When the <CodeStep step={2}>permalink</CodeStep> is provided to `useActionState`, the browser will navigate to the provided URL if the form is submitted before the JavaScript bundle loads. At the destination, render `useActionState` with the same Server Function and permalink so React can pass the returned state to it.
265+
If you pass the <CodeStep step={2}>`permalink`</CodeStep> to `useActionState`, the browser navigates to that URL when the form is submitted before the JavaScript bundle loads. At the destination, render the same form component with the same Server Function and `permalink` so React can pass the returned state to it.
254266

255267
For more, see the docs for [`useActionState`](/reference/react/useActionState).

0 commit comments

Comments
 (0)