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
+37-42Lines changed: 37 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,9 @@ Server Functions allow Client Components to call async functions executed on the
20
20
21
21
<Note>
22
22
23
-
#### How do I build support for Server Functions? {/*how-do-i-build-support-for-server-functions*/}
23
+
#### How do I build support for Server Functions? {/*how-do-i-build-support-for-server-functions*/}
24
24
25
-
While Server Functions in React 19 are stable and will not break between minor versions, the underlying APIs used to implement Server Functions in a React Server Components bundler or framework do not follow semver and may break between minors in React 19.x.
25
+
While Server Functions in React 19 are stable and will not break between minor versions, the underlying APIs used to implement Server Functions in a React Server Components bundler or framework do not follow semver and may break between minors in React 19.x.
26
26
27
27
To support Server Functions as a bundler or framework, we recommend pinning to a specific React version, or using the Canary release. We will continue working with bundlers and frameworks to stabilize the APIs used to implement Server Functions in the future.
28
28
@@ -32,77 +32,75 @@ When a Server Function is defined with the [`"use server"`](/reference/rsc/use-s
32
32
33
33
Server Functions can be created in Server Components and passed as props to Client Components, or they can be imported and used in Client Components.
34
34
35
-
## Usage {/*usage*/}
35
+
## Usage {/*usage*/}
36
36
37
-
### Creating a Server Function from a Server Component {/*creating-a-server-function-from-a-server-component*/}
37
+
### Creating a Server Function from a Server Component {/*creating-a-server-function-from-a-server-component*/}
38
38
39
39
Server Components can define Server Functions with the `"use server"` directive:
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
70
71
-
72
-
### Importing Server Functions from Client Components {/*importing-server-functions-from-client-components*/}
71
+
### Importing Server Functions from Client Components {/* 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
75
```js [[1, 3, "createNote"]]
77
-
"use server";
76
+
'use server';
78
77
79
78
exportasyncfunctioncreateNote() {
80
79
awaitdb.notes.create();
81
80
}
82
-
83
81
```
84
82
85
83
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:
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.
172
169
173
170
For more, see the docs for [Server Functions in Forms](/reference/rsc/use-server#server-functions-in-forms).
174
171
175
-
### Server Functions with `useActionState` {/*server-functions-with-use-action-state*/}
172
+
### Server Functions with `useActionState` {/*server-functions-with-use-action-state*/}
176
173
177
174
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.
197
196
198
-
For more, see the docs for [`useActionState`](/reference/react-dom/hooks/useFormState).
197
+
For more, see the docs for [`useActionState`](/reference/react/useActionState).
199
198
200
-
### Progressive enhancement with `useActionState` {/*progressive-enhancement-with-useactionstate*/}
199
+
### Progressive enhancement with `useActionState` {/*progressive-enhancement-with-useactionstate*/}
201
200
202
201
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.
221
216
222
-
For more, see the docs for [`useActionState`](/reference/react-dom/hooks/useFormState).
217
+
For more, see the docs for [`useActionState`](/reference/react/useActionState).
0 commit comments