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/react-dom/components/form.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,12 +93,14 @@ export default function Search() {
93
93
94
94
### Handle form submission with a Server Action {/*handle-form-submission-with-a-server-action*/}
95
95
96
+
96
97
Render a `<form>` with an input and submit button. Pass a Server Action (a function marked with [`'use server'`](/reference/react/use-server)) to the `action` prop of form to run the function when the form is submitted.
97
98
98
99
Passing a Server Action to `<form action>` allow users to submit forms without JavaScript enabled or before the code has loaded. This is beneficial to users who have a slow connection, device, or have JavaScript disabled and is similar to the way forms work when a URL is passed to the `action` prop.
99
100
100
101
You can use hidden form fields to provide data to the `<form>`'s action. The Server Action will be called with the hidden form field data as an instance of [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData).
101
102
103
+
102
104
```jsx
103
105
import { updateCart } from'./lib.js';
104
106
@@ -273,7 +275,9 @@ export async function deliverMessage(message) {
273
275
274
276
</Sandpack>
275
277
278
+
276
279
[//]: #'Uncomment the next line, and delete this line after the `useOptimistic` reference documentatino page is published'
280
+
277
281
[//]: #'To learn more about the `useOptimistic` Hook see the [reference documentation](/reference/react/hooks/useOptimistic).'
278
282
279
283
### Handling form submission errors {/*handling-form-submission-errors*/}
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/hooks/useFormState.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,8 +51,10 @@ function StatefulForm({}) {
51
51
52
52
The form state is the value returned by the action when the form was last submitted. If the form has not yet been submitted, it is the initial state that you pass.
53
53
54
+
54
55
If used with a Server Action, `useFormState` allows the server's response from submitting the form to be shown even before hydration has completed.
55
56
57
+
56
58
[See more examples below.](#usage)
57
59
58
60
#### Parameters {/*parameters*/}
@@ -117,8 +119,10 @@ function action(currentState, formData) {
117
119
118
120
#### Display form errors {/*display-form-errors*/}
119
121
122
+
120
123
To display messages such as an error message or toast that's returned by a Server Action, wrap the action in a call to `useFormState`.
121
124
125
+
122
126
<Sandpack>
123
127
124
128
```js App.js
@@ -190,8 +194,10 @@ form button {
190
194
191
195
#### Display structured information after submitting a form {/*display-structured-information-after-submitting-a-form*/}
192
196
197
+
193
198
The return value from a Server Action can be any serializable value. For example, it could be an object that includes a boolean indicating whether the action was successful, an error message, or updated information.
Copy file name to clipboardExpand all lines: src/content/reference/react/use-client.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,15 +51,18 @@ As dependencies of `RichTextEditor`, `formatDate` and `Button` will also be eval
51
51
* When a `'use client'` module is imported from another client-rendered module, the directive has no effect.
52
52
* When a component module contains a `'use client'` directive, any usage of that component is guaranteed to be a Client Component. However, a component can still be evaluated on the client even if it does not have a `'use client'` directive.
53
53
* A component usage is considered a Client Component if it is defined in module with `'use client'` directive or when it is a transitive dependency of a module that contains a `'use client'` directive. Otherwise, it is a Server Component.
54
+
54
55
* Code that is marked for client evaluation is not limited to components. All code that is a part of the Client module sub-tree is sent to and run by the client.
55
56
* When a server evaluated module imports values from a `'use client'` module, the values must either be a React component or [supported serializable prop values](#passing-props-from-server-to-client-components) to be passed to a Client Component. Any other use case will throw an exception.
56
57
57
58
### How `'use client'` marks client code {/*how-use-client-marks-client-code*/}
58
59
59
60
In a React app, components are often split into separate files, or [modules](/learn/importing-and-exporting-components#exporting-and-importing-a-component).
60
61
62
+
61
63
For apps that use React Server Components, the app is server-rendered by default. `'use client'` introduces a server-client boundary in the [module dependency tree](/learn/understanding-your-ui-as-a-tree#the-module-dependency-tree), effectively creating a subtree of Client modules.
62
64
65
+
63
66
To better illustrate this, consider the following React Server Components app.
64
67
65
68
<Sandpack>
@@ -145,8 +148,10 @@ export default [
145
148
146
149
</Sandpack>
147
150
151
+
148
152
In the module dependency tree of this example app, the `'use client'` directive in `InspirationGenerator.js` marks that module and all of its transitive dependencies as Client modules. The subtree starting at `InspirationGenerator.js` is now marked as Client modules.
149
153
154
+
150
155
<Diagramname="use_client_module_dependency"height={250}width={545}alt="A tree graph with the top node representing the module 'App.js'. 'App.js' has three children: 'Copyright.js', 'FancyText.js', and 'InspirationGenerator.js'. 'InspirationGenerator.js' has two children: 'FancyText.js' and 'inspirations.js'. The nodes under and including 'InspirationGenerator.js' have a yellow background color to signify that this sub-graph is client-rendered due to the 'use client' directive in 'InspirationGenerator.js'.">
151
156
`'use client'` segments the module dependency tree of the React Server Components app, marking `InspirationGenerator.js` and all of its dependencies as client-rendered.
152
157
</Diagram>
@@ -237,7 +242,9 @@ With `'use client'`, you can determine when components are Client Components. As
237
242
For simplicity, we talk about Server Components, but the same principles apply to all code in your app that is server run.
238
243
239
244
#### Advantages of Server Components {/*advantages*/}
245
+
240
246
* Server Components can reduce the amount of code sent and run by the client. Only Client modules are bundled and evaluated by the client.
247
+
241
248
* Server Components benefit from running on the server. They can access the local filesystem and may experience low latency for data fetches and network requests.
242
249
243
250
#### Limitations of Server Components {/*limitations*/}
@@ -269,7 +276,9 @@ Serializable props include:
269
276
* [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) and [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
* Plain [objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object): those created with [object initializers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer), with serializable properties
279
+
272
280
* Functions that are [Server Actions](/reference/react/use-server)
As `Counter` requires both the `useState` Hook and event handlers to increment or decrement the value, this component must be a Client Component and will require a `'use client'` directive at the top.
311
321
322
+
312
323
In contrast, a component that renders UI without interaction will not need to be a Client Component.
313
324
314
325
```js
@@ -376,4 +387,6 @@ These libraries may rely on component Hooks or client APIs. Third-party componen
376
387
377
388
If these libraries have been updated to be compatible with React Server Components, then they will already include `'use client'` markers of their own, allowing you to use them directly from your Server Components. If a library hasn't been updated, or if a component needs props like event handlers that can only be specified on the client, you may need to add your own Client Component file in between the third-party Client Component and your Server Component where you'd like to use it.
Copy file name to clipboardExpand all lines: src/content/reference/react/use-server.md
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,15 +25,18 @@ canary: true
25
25
26
26
### `'use server'` {/*use-server*/}
27
27
28
+
28
29
Add `'use server'` at the top of an async function body to mark the function as callable by the client. We call these functions _Server Actions_.
29
30
31
+
30
32
```js {2}
31
33
asyncfunctionaddToCart(data) {
32
34
'use server';
33
35
// ...
34
36
}
35
37
```
36
38
39
+
37
40
When calling a Server Action on the client, it will make a network request to the server that includes a serialized copy of any arguments passed. If the Server Action returns a value, that value will be serialized and returned to the client.
38
41
39
42
Instead of individually marking functions with `'use server'`, you can add the directive to the top of a file to mark all exports within that file as Server Actions that can be used anywhere, including imported in client code.
@@ -57,16 +60,19 @@ In any Server Action, make sure to validate that the logged-in user is allowed t
57
60
58
61
To prevent sending sensitive data from a Server Action, there are experimental taint APIs to prevent unique values and objects from being passed to client code.
59
62
63
+
60
64
See [experimental_taintUniqueValue](/reference/react/experimental_taintUniqueValue) and [experimental_taintObjectReference](/reference/react/experimental_taintObjectReference).
61
65
62
66
</Wip>
63
67
64
68
### Serializable arguments and return values {/*serializable-parameters-and-return-values*/}
65
69
70
+
66
71
As client code calls the Server Action over the network, any arguments passed will need to be serializable.
67
72
68
73
Here are supported types for Server Action arguments:
* Plain [objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object): those created with [object initializers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer), with serializable properties
* Objects that are instances of any class (other than the built-ins mentioned) or objects with [a null prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#null-prototype_objects)
95
105
* Symbols not registered globally, ex. `Symbol('my new symbol')`
@@ -100,10 +110,12 @@ Supported serializable return values are the same as [serializable props](/refer
100
110
101
111
## Usage {/*usage*/}
102
112
113
+
103
114
### Server Actions in forms {/*server-actions-in-forms*/}
104
115
105
116
The most common use case of Server Actions will be calling server functions that mutate data. On the browser, the [HTML form element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) is the traditional approach for a user to submit a mutation. With React Server Components, React introduces first-class support for Server Actions in [forms](/reference/react-dom/components/form).
106
117
118
+
107
119
Here is a form that allows a user to request a username.
108
120
109
121
```js [[1, 3, "formData"]]
@@ -123,16 +135,20 @@ export default App() {
123
135
}
124
136
```
125
137
138
+
126
139
In this example `requestUsername` is a Server Action passed to a `<form>`. When a user submits this form, there is a network request to the server function `requestUsername`. When calling a Server Action in a form, React will supply the form's <CodeStepstep={1}>[FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData)</CodeStep> as the first argument to the Server Action.
127
140
128
141
By passing a Server Action to the form `action`, React can [progressively enhance](https://developer.mozilla.org/en-US/docs/Glossary/Progressive_Enhancement) the form. This means that forms can be submitted before the JavaScript bundle is loaded.
129
142
143
+
130
144
#### Handling return values in forms {/*handling-return-values*/}
131
145
132
146
In the username request form, there might be the chance that a username is not available. `requestUsername` should tell us if it fails or not.
133
147
148
+
134
149
To update the UI based on the result of a Server Action while supporting progressive enhancement, use [`useFormState`](/reference/react-dom/hooks/useFormState).
135
150
151
+
136
152
```js
137
153
// requestUsername.js
138
154
'use server';
@@ -171,12 +187,14 @@ function UsernameForm() {
171
187
172
188
Note that like most Hooks, `useFormState` can only be called in <CodeStepstep={1}>[client code](/reference/react/use-client)</CodeStep>.
173
189
190
+
174
191
### Calling a Server Action outside of `<form>` {/*calling-a-server-action-outside-of-form*/}
175
192
176
193
Server Actions are exposed server endpoints and can be called anywhere in client code.
177
194
178
195
When using a Server Action outside of a [form](/reference/react-dom/components/form), call the Server Action in a [transition](/reference/react/useTransition), which allows you to display a loading indicator, show [optimistic state updates](/reference/react/useOptimistic), and handle unexpected errors. Forms will automatically wrap Server Actions in transitions.
0 commit comments