You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 912781a
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: src/content/reference/rsc/server-functions.md
+28-16Lines changed: 28 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,8 @@ For more, see the docs for [`"use server"`](/reference/rsc/use-server).
72
72
73
73
Client Components can import Server Functions from files that use the `"use server"` directive:
74
74
75
-
```js [[1, 3, "createNote"]]
75
+
```js [[1, 4, "createNote"]]
76
+
// actions.js
76
77
"use server";
77
78
78
79
exportasyncfunctioncreateNote() {
@@ -82,12 +83,15 @@ export async function createNote() {
82
83
83
84
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:
// 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.
129
136
startTransition(() => {
130
137
setError(error);
131
138
if (!error) {
@@ -154,13 +161,14 @@ This allows you to access the `isPending` state of the Server Function by wrappi
154
161
155
162
For more, see the docs for [Calling a Server Function outside of `<form>`](/reference/rsc/use-server#calling-a-server-function-outside-of-form).
156
163
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*/}
158
165
159
166
Server Functions work with the new Form features in React 19.
160
167
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:
162
169
163
-
```js [[1, 3, "updateName"]]
170
+
```js [[1, 4, "updateName"]]
171
+
// actions.js
164
172
"use server";
165
173
166
174
exportasyncfunctionupdateName(formData) {
@@ -172,7 +180,8 @@ export async function updateName(formData) {
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.
190
199
191
200
For more, see the docs for [Server Functions in Forms](/reference/rsc/use-server#server-functions-in-forms).
192
201
193
202
### Calling a Server Function with `useActionState` {/*server-functions-with-use-action-state*/}
194
203
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:
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.
229
240
230
241
For more, see the docs for [`useActionState`](/reference/react/useActionState).
231
242
232
243
### Supporting progressive enhancement with `useActionState` {/*progressive-enhancement-with-useactionstate*/}
233
244
234
245
Server Functions also support progressive enhancement with the third argument of `useActionState`.
When the <CodeStepstep={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 <CodeStepstep={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.
254
266
255
267
For more, see the docs for [`useActionState`](/reference/react/useActionState).
0 commit comments