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/use-server.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,28 +4,28 @@ title: "'use server'"
4
4
5
5
<Wip>
6
6
7
-
This section is incomplete.
7
+
此章节仍在完善中。
8
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.
`'use server'`marks server-side functions that can be called from client-side code.
16
+
`'use server'`标记可以从客户端代码调用的服务器函数。
17
17
18
18
</Intro>
19
19
20
20
<InlineToc />
21
21
22
22
---
23
23
24
-
## Reference {/*reference*/}
24
+
## 参考 {/*reference*/}
25
25
26
26
### `'use server'` {/*use-server*/}
27
27
28
-
Add`'use server';` at the very top of an async function to mark that the function can be executed by the client.
28
+
在异步函数的最顶部添加`'use server'` 以标记该函数可以被客户端执行。
29
29
30
30
```js
31
31
asyncfunctionaddToCart(data) {
@@ -36,13 +36,13 @@ async function addToCart(data) {
36
36
// <ProductDetailPage addToCart={addToCart} />
37
37
```
38
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.
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.
*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.)
0 commit comments