Skip to content

Commit 4717053

Browse files
authored
Merge branch 'main' into sync-fcd00068
2 parents 9ee7994 + f130505 commit 4717053

6 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/content/reference/react-dom/components/form.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ export default function Search() {
9393

9494
### Handle form submission with a Server Action {/*handle-form-submission-with-a-server-action*/}
9595

96+
9697
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.
9798

9899
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.
99100

100101
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).
101102

103+
102104
```jsx
103105
import { updateCart } from './lib.js';
104106

@@ -273,7 +275,9 @@ export async function deliverMessage(message) {
273275

274276
</Sandpack>
275277

278+
276279
[//]: # 'Uncomment the next line, and delete this line after the `useOptimistic` reference documentatino page is published'
280+
277281
[//]: # 'To learn more about the `useOptimistic` Hook see the [reference documentation](/reference/react/hooks/useOptimistic).'
278282

279283
### Handling form submission errors {/*handling-form-submission-errors*/}

src/content/reference/react-dom/hooks/useFormState.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ function StatefulForm({}) {
5151

5252
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.
5353

54+
5455
If used with a Server Action, `useFormState` allows the server's response from submitting the form to be shown even before hydration has completed.
5556

57+
5658
[See more examples below.](#usage)
5759

5860
#### Parameters {/*parameters*/}
@@ -117,8 +119,10 @@ function action(currentState, formData) {
117119

118120
#### Display form errors {/*display-form-errors*/}
119121

122+
120123
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`.
121124

125+
122126
<Sandpack>
123127

124128
```js App.js
@@ -190,8 +194,10 @@ form button {
190194

191195
#### Display structured information after submitting a form {/*display-structured-information-after-submitting-a-form*/}
192196

197+
193198
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.
194199

200+
195201
<Sandpack>
196202

197203
```js App.js

src/content/reference/react/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ title: React Reference Overview
44

55
<Intro>
66

7+
78
This section provides detailed reference documentation for working with React. For an introduction to React, please visit the [Learn](/learn) section.
89

910
</Intro>
@@ -32,3 +33,4 @@ React-dom contains features that are only supported for web applications (which
3233
## Legacy APIs {/*legacy-apis*/}
3334

3435
* [Legacy APIs](/reference/react/legacy) - Exported from the `react` package, but not recommended for use in newly written code.
36+

src/content/reference/react/use-client.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ As dependencies of `RichTextEditor`, `formatDate` and `Button` will also be eval
5151
* When a `'use client'` module is imported from another client-rendered module, the directive has no effect.
5252
* 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.
5353
* 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+
5455
* 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.
5556
* 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.
5657

5758
### How `'use client'` marks client code {/*how-use-client-marks-client-code*/}
5859

5960
In a React app, components are often split into separate files, or [modules](/learn/importing-and-exporting-components#exporting-and-importing-a-component).
6061

62+
6163
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.
6264

65+
6366
To better illustrate this, consider the following React Server Components app.
6467

6568
<Sandpack>
@@ -145,8 +148,10 @@ export default [
145148

146149
</Sandpack>
147150

151+
148152
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.
149153

154+
150155
<Diagram name="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'.">
151156
`'use client'` segments the module dependency tree of the React Server Components app, marking `InspirationGenerator.js` and all of its dependencies as client-rendered.
152157
</Diagram>
@@ -237,7 +242,9 @@ With `'use client'`, you can determine when components are Client Components. As
237242
For simplicity, we talk about Server Components, but the same principles apply to all code in your app that is server run.
238243

239244
#### Advantages of Server Components {/*advantages*/}
245+
240246
* 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+
241248
* 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.
242249

243250
#### Limitations of Server Components {/*limitations*/}
@@ -269,7 +276,9 @@ Serializable props include:
269276
* [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)
270277
* [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
271278
* 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+
272280
* Functions that are [Server Actions](/reference/react/use-server)
281+
273282
* Client or Server Component elements (JSX)
274283
* [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
275284

@@ -307,8 +316,10 @@ export default function Counter({initialValue = 0}) {
307316

308317
</Sandpack>
309318

319+
310320
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.
311321

322+
312323
In contrast, a component that renders UI without interaction will not need to be a Client Component.
313324

314325
```js
@@ -376,4 +387,6 @@ These libraries may rely on component Hooks or client APIs. Third-party componen
376387

377388
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.
378389

390+
379391
[TODO]: <> (Troubleshooting - need use-cases)
392+

src/content/reference/react/use-server.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ canary: true
2525

2626
### `'use server'` {/*use-server*/}
2727

28+
2829
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_.
2930

31+
3032
```js {2}
3133
async function addToCart(data) {
3234
'use server';
3335
// ...
3436
}
3537
```
3638

39+
3740
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.
3841

3942
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
5760

5861
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.
5962

63+
6064
See [experimental_taintUniqueValue](/reference/react/experimental_taintUniqueValue) and [experimental_taintObjectReference](/reference/react/experimental_taintObjectReference).
6165

6266
</Wip>
6367

6468
### Serializable arguments and return values {/*serializable-parameters-and-return-values*/}
6569

70+
6671
As client code calls the Server Action over the network, any arguments passed will need to be serializable.
6772

6873
Here are supported types for Server Action arguments:
6974

75+
7076
* Primitives
7177
* [string](https://developer.mozilla.org/en-US/docs/Glossary/String)
7278
* [number](https://developer.mozilla.org/en-US/docs/Glossary/Number)
@@ -84,12 +90,16 @@ Here are supported types for Server Action arguments:
8490
* [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
8591
* [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) instances
8692
* 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
93+
8794
* Functions that are Server Actions
95+
8896
* [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
8997

9098
Notably, these are not supported:
9199
* React elements, or [JSX](https://react.dev/learn/writing-markup-with-jsx)
100+
92101
* Functions, including component functions or any other function that is not a Server Action
102+
93103
* [Classes](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Classes_in_JavaScript)
94104
* 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)
95105
* Symbols not registered globally, ex. `Symbol('my new symbol')`
@@ -100,10 +110,12 @@ Supported serializable return values are the same as [serializable props](/refer
100110

101111
## Usage {/*usage*/}
102112

113+
103114
### Server Actions in forms {/*server-actions-in-forms*/}
104115

105116
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).
106117

118+
107119
Here is a form that allows a user to request a username.
108120

109121
```js [[1, 3, "formData"]]
@@ -123,16 +135,20 @@ export default App() {
123135
}
124136
```
125137

138+
126139
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 <CodeStep step={1}>[FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData)</CodeStep> as the first argument to the Server Action.
127140

128141
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.
129142

143+
130144
#### Handling return values in forms {/*handling-return-values*/}
131145

132146
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.
133147

148+
134149
To update the UI based on the result of a Server Action while supporting progressive enhancement, use [`useFormState`](/reference/react-dom/hooks/useFormState).
135150

151+
136152
```js
137153
// requestUsername.js
138154
'use server';
@@ -171,12 +187,14 @@ function UsernameForm() {
171187

172188
Note that like most Hooks, `useFormState` can only be called in <CodeStep step={1}>[client code](/reference/react/use-client)</CodeStep>.
173189

190+
174191
### Calling a Server Action outside of `<form>` {/*calling-a-server-action-outside-of-form*/}
175192

176193
Server Actions are exposed server endpoints and can be called anywhere in client code.
177194

178195
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.
179196

197+
180198
```js {9-12}
181199
import incrementLike from './actions';
182200
import { useState, useTransition } from 'react';
@@ -212,4 +230,6 @@ export default async incrementLike() {
212230
}
213231
```
214232

233+
215234
To read a Server Action return value, you'll need to `await` the promise returned.
235+

src/sidebarReference.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
},
8383
{
8484
"title": "useTransition",
85-
"path": "/reference/react/useTransition"
85+
"path": "/reference/react/useTransition",
86+
"canary": true
8687
}
8788
]
8889
},

0 commit comments

Comments
 (0)