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
Copy file name to clipboardExpand all lines: src/content/reference/rsc/server-functions.md
+59-31Lines changed: 59 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,33 +42,30 @@ Server Components can define Server Functions with the `"use server"` directive:
42
42
// Server Component
43
43
importButtonfrom'./Button';
44
44
45
-
functionEmptyNote() {
45
+
functionEmptyNote() {
46
46
asyncfunctioncreateNoteAction() {
47
47
// Server Function
48
48
'use server';
49
49
50
50
awaitdb.notes.create();
51
51
}
52
52
53
-
return<Button onClick={createNoteAction}/>;
53
+
return<Button onClick={createNoteAction}/>;
54
54
}
55
55
```
56
56
57
57
When React renders the `EmptyNote` Server Component, it will create a reference to the `createNoteAction` function, and pass that reference to the `Button` Client Component. When the button is clicked, React will send a request to the server to execute the `createNoteAction` function with the reference provided:
For more, see the docs for [`"use server"`](/reference/rsc/use-server).
70
68
71
-
72
69
### Importing Server Functions from Client Components {/*importing-server-functions-from-client-components*/}
73
70
74
71
Client Components can import Server Functions from files that use the `"use server"` directive:
@@ -79,19 +76,17 @@ Client Components can import Server Functions from files that use the `"use serv
79
76
exportasyncfunctioncreateNote() {
80
77
awaitdb.notes.create();
81
78
}
82
-
83
79
```
84
80
85
81
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:
This allows you to access the `isPending` state of the Server Function by wrapping it in an Action on the client.
149
151
150
-
For more, see the docs for [Calling a Server Function outside of `<form>`](/reference/rsc/use-server#calling-a-server-function-outside-of-form)
152
+
For more, see the docs for [Calling a Server Function outside of `<form>`](/reference/rsc/use-server#calling-a-server-function-outside-of-form).
151
153
152
154
### Server Functions with Form Actions {/*using-server-functions-with-form-actions*/}
153
155
154
156
Server Functions work with the new Form features in React 19.
155
157
156
-
You can pass a Server Function to a Form to automatically submit the form to the server:
158
+
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:
When the Form submission succeeds, React will automatically reset the form. You can add `useActionState` to access the pending state, last response, or to support progressive enhancement.
186
+
When the Form submission succeeds, React will automatically reset the uncontrolled fields in the form. Server Function forms can be submitted before the JavaScript bundle loads. You can add `useActionState` to access the pending state and last response.
174
187
175
188
For more, see the docs for [Server Functions in Forms](/reference/rsc/use-server#server-functions-in-forms).
176
189
177
190
### Server Functions with `useActionState` {/*server-functions-with-use-action-state*/}
178
191
179
-
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:
192
+
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:
When using `useActionState` with Server Functions, React will also automatically replay form submissions entered before hydration finishes. This means users can interact with your app even before the app has hydrated.
225
+
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.
199
226
200
227
For more, see the docs for [`useActionState`](/reference/react/useActionState).
201
228
202
229
### Progressive enhancement with `useActionState` {/*progressive-enhancement-with-useactionstate*/}
203
230
204
231
Server Functions also support progressive enhancement with the third argument of `useActionState`.
When the <CodeStepstep={2}>permalink</CodeStep> is provided to `useActionState`, React will redirect to the provided URL if the form is submitted before the JavaScript bundle loads.
250
+
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. The same form component, including the same Server Function and permalink, must be rendered at the destination so React can pass the returned state to it.
223
251
224
252
For more, see the docs for [`useActionState`](/reference/react/useActionState).
0 commit comments