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 2177e28
Browse filesBrowse the repository at this point in the historyBrowse files
authored
Fix Server Function examples for Actions, forms, and useActionState (#8538)
* Fix Server Functions documentation examples
* Fix code snippet highlights
Corrected the index of 'submitAction' in the code snippet.
* Clarify wording
* Update section titles for clarity
* Polish Server Functions examples: consistent headings, reset wording, transition comment
- Convert the remaining two Usage headings to gerund form to match the
rest of the page and react.dev convention (anchors unchanged)
- Reset wording: "succeeds" + "the form's uncontrolled fields" (aligns with #8512)
- Comment the startTransition-after-await; bump CodeStep line accordingly
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* Polish Server Functions examples
* Fix Server Functions code highlight
---------
Co-authored-by: Aurora Scharff <aurora.sofie@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
For more, see the docs for [`"use server"`](/reference/rsc/use-server).
70
70
71
-
72
-
### Importing Server Functions from Client Components {/*importing-server-functions-from-client-components*/}
71
+
### Importing a Server Function into a Client Component {/*importing-server-functions-from-client-components*/}
73
72
74
73
Client Components can import Server Functions from files that use the `"use server"` directive:
75
74
76
-
```js [[1, 3, "createNote"]]
75
+
```js [[1, 4, "createNote"]]
76
+
// actions.js
77
77
"use server";
78
78
79
79
exportasyncfunctioncreateNote() {
80
80
awaitdb.notes.create();
81
81
}
82
-
83
82
```
84
83
85
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:
This allows you to access the `isPending` state of the Server Function by wrapping it in an Action on the client.
149
161
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)
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).
151
163
152
-
### Server Functions with Form Actions {/*using-server-functions-with-form-actions*/}
164
+
### Passing a Server Function to the `<form>``action` prop {/*using-server-functions-with-form-actions*/}
153
165
154
166
Server Functions work with the new Form features in React 19.
155
167
156
-
You can pass a Server Function to a Form to automatically submit the form to the server:
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:
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.
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.
174
199
175
200
For more, see the docs for [Server Functions in Forms](/reference/rsc/use-server#server-functions-in-forms).
176
201
177
-
### Server Functions with `useActionState` {/*server-functions-with-use-action-state*/}
202
+
### Calling a Server Function with `useActionState` {/*server-functions-with-use-action-state*/}
203
+
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:
205
+
206
+
```js [[1, 4, "updateName"]]
207
+
// actions.js
208
+
"use server";
178
209
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:
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.
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.
199
240
200
241
For more, see the docs for [`useActionState`](/reference/react/useActionState).
201
242
202
-
### Progressive enhancement with `useActionState` {/*progressive-enhancement-with-useactionstate*/}
243
+
### Supporting progressive enhancement with `useActionState` {/*progressive-enhancement-with-useactionstate*/}
203
244
204
245
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.
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.
223
266
224
267
For more, see the docs for [`useActionState`](/reference/react/useActionState).
0 commit comments