Skip to content

Commit b585e0a

Browse files
authored
Merge pull request #11 from reactjs/sync-292534e9
Sync with react.dev @ 292534e
2 parents d5c7f59 + dce9222 commit b585e0a

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: "Directives"
3+
---
4+
5+
<Intro>
6+
7+
React uses two directives to provide instructions to [bundlers compatible with React Server Components](/learn/start-a-new-react-project#bleeding-edge-react-frameworks).
8+
9+
</Intro>
10+
11+
---
12+
13+
## Source code directives {/*source-code-directives*/}
14+
15+
* [`'use client'`](/reference/react/use-client) marks source files whose components execute on the client.
16+
* [`'use server'`](/reference/react/use-server) marks server-side functions that can be called from client-side code.
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: "'use client'"
3+
---
4+
5+
<Note>
6+
7+
These directives are needed only if you're [using React Server Components](/learn/start-a-new-react-project#bleeding-edge-react-frameworks) or building a library compatible with them.
8+
9+
</Note>
10+
11+
12+
<Intro>
13+
14+
`'use client'` marks source files whose components execute on the client.
15+
16+
</Intro>
17+
18+
<InlineToc />
19+
20+
---
21+
22+
## Reference {/*reference*/}
23+
24+
### `'use client'` {/*use-client*/}
25+
26+
Add `'use client';` at the very top of a file to mark that the file (including any child components it uses) executes on the client, regardless of where it's imported.
27+
28+
```js
29+
'use client';
30+
31+
import { useState } from 'react';
32+
33+
export default function RichTextEditor(props) {
34+
// ...
35+
```
36+
37+
When a file marked `'use client'` is imported from a server component, [compatible bundlers](/learn/start-a-new-react-project#bleeding-edge-react-frameworks) will treat the import as the "cut-off point" between server-only code and client code. Components at or below this point in the module graph can use client-only React features like [`useState`](/reference/react/useState).
38+
39+
#### Caveats {/*caveats*/}
40+
41+
* It's not necessary to add `'use client'` to every file that uses client-only React features, only the files that are imported from server component files. `'use client'` denotes the _boundary_ between server-only and client code; any components further down the tree will automatically be executed on the client. In order to be rendered from server components, components exported from `'use client'` files must have serializable props.
42+
* When a `'use client'` file is imported from a server file, the imported values can be rendered as a React component or passed via props to a client component. Any other use will throw an exception.
43+
* When a `'use client'` file is imported from another client file, the directive has no effect. This allows you to write client-only components that are simultaneously usable from server and client components.
44+
* All the code in `'use client'` file as well as any modules it imports (directly or indirectly) will become a part of the client module graph and must be sent to and executed by the client in order to be rendered by the browser. To reduce client bundle size and take full advantage of the server, move state (and the `'use client'` directives) lower in the tree when possible, and pass rendered server components [as children](/learn/passing-props-to-a-component#passing-jsx-as-children) to client components.
45+
* Because props are serialized across the server–client boundary, note that the placement of these directives can affect the amount of data sent to the client; avoid data structures that are larger than necessary.
46+
* Components like a `<MarkdownRenderer>` that use neither server-only nor client-only features should generally not be marked with `'use client'`. That way, they can render exclusively on the server when used from a server component, but they'll be added to the client bundle when used from a client component.
47+
* Libraries published to npm should include `'use client'` on exported React components that can be rendered with serializable props that use client-only React features, to allow those components to be imported and rendered by server components. Otherwise, users will need to wrap library components in their own `'use client'` files which can be cumbersome and prevents the library from moving logic to the server later. When publishing prebundled files to npm, ensure that `'use client'` source files end up in a bundle marked with `'use client'`, separate from any bundle containing exports that can be used directly on the server.
48+
* Client components will still run as part of server-side rendering (SSR) or build-time static site generation (SSG), which act as clients to transform React components' initial render output to HTML that can be rendered before JavaScript bundles are downloaded. But they can't use server-only features like reading directly from a database.
49+
* Directives like `'use client'` must be at the very beginning of a file, above any imports or other code (comments above directives are OK). They must be written with single or double quotes, not backticks. (The `'use xyz'` directive format somewhat resembles the `useXyz()` Hook naming convention, but the similarity is coincidental.)
50+
51+
## Usage {/*usage*/}
52+
53+
<Wip>
54+
55+
This section is incomplete. See also the [Next.js documentation for Server Components](https://beta.nextjs.org/docs/rendering/server-and-client-components).
56+
57+
</Wip>
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: "'use server'"
3+
---
4+
5+
<Wip>
6+
7+
This section is incomplete.
8+
9+
These directives are needed only if you're [using React Server Components](/learn/start-a-new-react-project#bleeding-edge-react-frameworks) or building a library compatible with them.
10+
11+
</Wip>
12+
13+
14+
<Intro>
15+
16+
`'use server'` marks server-side functions that can be called from client-side code.
17+
18+
</Intro>
19+
20+
<InlineToc />
21+
22+
---
23+
24+
## Reference {/*reference*/}
25+
26+
### `'use server'` {/*use-server*/}
27+
28+
Add `'use server';` at the very top of an async function to mark that the function can be executed by the client.
29+
30+
```js
31+
async function addToCart(data) {
32+
'use server';
33+
// ...
34+
}
35+
36+
// <ProductDetailPage addToCart={addToCart} />
37+
```
38+
39+
This function can be passed to the client. When called on the client, it will make a network request to the server that includes a serialized copy of any arguments passed. If the server function returns a value, that value will be serialized and returned to the client.
40+
41+
Alternatively, add `'use server';` at the very top of a file to mark all exports within that file as async server functions that can be used anywhere, including imported in client component files.
42+
43+
#### Caveats {/*caveats*/}
44+
45+
* Remember that parameters to functions marked with `'use server'` are fully client-controlled. For security, always treat them as untrusted input, making sure to validate and escape the arguments as appropriate.
46+
* To avoid the confusion that might result from mixing client- and server-side code in the same file, `'use server'` can only be used in server-side files; the resulting functions can be passed to client components through props.
47+
* Because the underlying network calls are always asynchronous, `'use server'` can be used only on async functions.
48+
* Directives like `'use server'` must be at the very beginning of their function or file, above any other code including imports (comments above directives are OK). They must be written with single or double quotes, not backticks. (The `'use xyz'` directive format somewhat resembles the `useXyz()` Hook naming convention, but the similarity is coincidental.)

src/content/reference/react/useRef.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function MyComponent() {
228228
229229
If you *have to* read [or write](/reference/react/useState#storing-information-from-previous-renders) something during rendering, [use state](/reference/react/useState) instead.
230230
231-
When you break these rules, your component might still work, but most of the newer features we're adding to React will rely on these expectations. Read more about [keeping your components pure.](/learn/keeping-components-pure#where-you-can-cause-side-effects)
231+
When you break these rules, your component might still work, but most of the newer features we're adding to React will rely on these expectations. Read more about [keeping your components pure.](/learn/keeping-components-pure#where-you-_can_-cause-side-effects)
232232
233233
</Pitfall>
234234

src/sidebarReference.json

+14
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@
120120
}
121121
]
122122
},
123+
{
124+
"title": "Directives",
125+
"path": "/reference/react/directives",
126+
"routes": [
127+
{
128+
"title": "'use client'",
129+
"path": "/reference/react/use-client"
130+
},
131+
{
132+
"title": "'use server'",
133+
"path": "/reference/react/use-server"
134+
}
135+
]
136+
},
123137
{
124138
"hasSectionHeader": true,
125139
"sectionHeader": "[email protected]"

0 commit comments

Comments
 (0)