Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cn): translate reference/react/use-server into Chinese #1378

Merged
merged 15 commits into from
Nov 17, 2023
Merged
126 changes: 63 additions & 63 deletions src/content/reference/react/use-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ canary: true

<Canary>

这些指令仅在你 [使用 React 服务器组件](/learn/start-a-new-react-project#bleeding-edge-react-frameworks) 或构建可适配库时需要。
`'use server'` 仅在 [使用 React 服务器组件](/learn/start-a-new-react-project#bleeding-edge-react-frameworks) 或构建可适配库时需要。

</Canary>

Expand All @@ -25,7 +25,7 @@ canary: true

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

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_.
在异步函数顶部添加 `'use server'` 以将该函数标记为可由客户端调用。我们将这些函数称为 **Server Action**。

```js {2}
async function addToCart(data) {
Expand All @@ -34,77 +34,77 @@ async function addToCart(data) {
}
```

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.
在客户端调用服务器动作时,它将向服务器发出网络请求,其中包含传递的任何参数的序列化副本。如果服务器动作返回一个值,该值将被序列化并返回给客户端。
Yucohny marked this conversation as resolved.
Show resolved Hide resolved

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.
可以将 `'use server'` 指令添加到文件顶部,而不是逐个标记函数为 `'use server'`,以将文件中的所有导出都标记为可在任何地方使用的 Server Action,包括在客户端代码中导入。
Yucohny marked this conversation as resolved.
Show resolved Hide resolved

#### Caveats {/*caveats*/}
* `'use server'` must be at the very beginning of their function or module; above any other code including imports (comments above directives are OK). They must be written with single or double quotes, not backticks.
* `'use server'` can only be used in server-side files. The resulting Server Actions can be passed to Client Components through props. See supported [types for serialization](#serializable-parameters-and-return-values).
* To import a Server Action from [client code](/reference/react/use-client), the directive must be used on a module level.
* Because the underlying network calls are always asynchronous, `'use server'` can only be used on async functions.
* Always treat arguments to Server Actions as untrusted input and authorize any mutations. See [security considerations](#security).
* Server Actions should be called in a [transition](/reference/react/useTransition). Server Actions passed to [`<form action>`](/reference/react-dom/components/form#props) or [`formAction`](/reference/react-dom/components/input#props) will automatically be called in a transition.
* Server Actions are designed for mutations that update server-side state; they are not recommended for data fetching. Accordingly, frameworks implementing Server Actions typically process one action at a time and do not have a way to cache the return value.
#### 注意 {/*caveats*/}
* `'use server'` 必须位于文件的最前方,位于任何导入或其他代码之上(可以位于代码顶部的注释之下)。它们必须用单引号或双引号编写,但不能用反引号。
Yucohny marked this conversation as resolved.
Show resolved Hide resolved
* `'use server'` 只能在服务器端文件中使用。生成的 Server Action 可以通过 props 传递给客户端组件。请参阅支持的 [序列化参数和返回值类型](#serializable-parameters-and-return-values)
* 要从 [客户端代码](/reference/react/use-client) 导入 Server Action,必须在模块级别使用该指令。
* 由于底层网络调用始终是异步的,`'use server'` 只能用于异步函数。
* 始终将服务器动作的参数视为不受信任的输入,并授权任何变更。请参阅 [安全考虑](#security)
* 应在 [transition](/reference/react/useTransition) 中调用 Server Action。传递给 [`<form action>`](/reference/react-dom/components/form#props) [`formAction`](/reference/react-dom/components/input#props) 的 Server Action 将自动在 transition 中被调用。
* Server Action 专为更新服务器端状态的变更而设计,不建议用于数据获取。因此,实现 Server Action 的框架通常一次只处理一个 Action,没有缓存返回值的方式。

### Security considerations {/*security*/}
### 安全考虑 {/*security*/}

Arguments to Server Actions are fully client-controlled. For security, always treat them as untrusted input, and make sure to validate and escape arguments as appropriate.
Server Action 的参数完全由客户端控制。出于安全考虑,始终将它们视为不受信任的输入,并确保根据需要验证和转义参数。

In any Server Action, make sure to validate that the logged-in user is allowed to perform that action.
在任何 Server Action 中,请确保验证已登录的用户是否被允许执行该操作。

<Wip>

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.
为防止从 Server Action 中发送敏感数据,提供了实验性的污染 API 用于阻止唯一值和对象传递到客户端代码。

See [experimental_taintUniqueValue](/reference/react/experimental_taintUniqueValue) and [experimental_taintObjectReference](/reference/react/experimental_taintObjectReference).
请参阅 [experimental_taintUniqueValue](/reference/react/experimental_taintUniqueValue) [experimental_taintObjectReference](/reference/react/experimental_taintObjectReference) 以了解更多。

</Wip>

### Serializable arguments and return values {/*serializable-parameters-and-return-values*/}
### 可序列化参数和返回值 {/*serializable-parameters-and-return-values*/}

As client code calls the Server Action over the network, any arguments passed will need to be serializable.
当客户端代码通过网络调用 Server Action 时,传递的任何参数都必须是可序列化的。

Here are supported types for Server Action arguments:
以下是 Server Action 参数支持的类型:

* Primitives
* [string](https://developer.mozilla.org/en-US/docs/Glossary/String)
* [number](https://developer.mozilla.org/en-US/docs/Glossary/Number)
* [bigint](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
* [boolean](https://developer.mozilla.org/en-US/docs/Glossary/Boolean)
* [undefined](https://developer.mozilla.org/en-US/docs/Glossary/Undefined)
* [null](https://developer.mozilla.org/en-US/docs/Glossary/Null)
* [symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol), only symbols registered in the global Symbol registry via [`Symbol.for`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for)
* Iterables containing serializable values
* [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)
* [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
* [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
* [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)
* [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)
* [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
* [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) instances
* 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
* Functions that are Server Actions
* [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
* 原始类型
* [string](https://developer.mozilla.org/zh-CN/docs/Glossary/String)
* [number](https://developer.mozilla.org/zh-CN/docs/Glossary/Number)
* [bigint](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
* [boolean](https://developer.mozilla.org/zh-CN/docs/Glossary/Boolean)
* [undefined](https://developer.mozilla.org/zh-CN/docs/Glossary/Undefined)
* [null](https://developer.mozilla.org/zh-CN/docs/Glossary/Null)
* [symbol](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol),仅包含在全局符号注册表中使用 [`Symbol.for`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for) 注册的符号。
* 包含可序列化值的迭代类型
* [String](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String)
* [Array](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array)
* [Map](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map)
* [Set](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set)
* [TypedArray](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) [ArrayBuffer](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
* [Date](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date)
* [FormData](https://developer.mozilla.org/zh-CN/docs/Web/API/FormData) 实例
* 普通 [对象](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object):使用 [对象初始化器](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Object_initializer) 创建的、具有可序列化属性
awxiaoxian2020 marked this conversation as resolved.
Show resolved Hide resolved
* 充当 Server Action 的函数
* [Promise](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise)

Notably, these are not supported:
* React elements, or [JSX](https://react.dev/learn/writing-markup-with-jsx)
* Functions, including component functions or any other function that is not a Server Action
* [Classes](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Classes_in_JavaScript)
* 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)
* Symbols not registered globally, ex. `Symbol('my new symbol')`
值得注意的是,以下内容不受支持:
* React 元素或 [JSX](https://react.dev/learn/writing-markup-with-jsx)
* 函数,包括组件函数和其他并非 Server Action 的函数
* [](https://developer.mozilla.org/zh-CN/docs/Learn/JavaScript/Objects/Classes_in_JavaScript)
* 任何类的实例对象(除了提到的内置类)或具有 [null 原型](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object#null-prototype_objects) 的对象
Yucohny marked this conversation as resolved.
Show resolved Hide resolved
* 未全局注册的符号,例如 `Symbol('my new symbol')`


Supported serializable return values are the same as [serializable props](/reference/react/use-client#passing-props-from-server-to-client-components) for a boundary Client Component.
支持的可序列化返回值与边界客户端组件的 [可序列化 props](/reference/react/use-client#passing-props-from-server-to-client-components) 相同。


## 用法 {/*usage*/}

### Server Actions in forms {/*server-actions-in-forms*/}
### 表格中的 Server Action {/*server-actions-in-forms*/}

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).
Server Action 的最常见用法将是调用会更改数据的服务器函数。在浏览器中,[HTML form 元素](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) 是用户提交变更的传统方法。通过 React 服务器组件,React 在 [表单](/reference/react-dom/components/form) 中首次引入了对 Server Action 的一流支持。

Here is a form that allows a user to request a username.
以下是一个允许用户请求用户名的表单。

```js [[1, 3, "formData"]]
// App.js
Expand All @@ -123,15 +123,15 @@ export default App() {
}
```

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.
在这个示例中,`requestUsername` 是一个传递给 `<form>` 的 Server Action。当用户提交此表单时,会发起一个网络请求到服务器函数 `requestUsername`。在调用表单中的 Server Action 时,React 将 `FormData` 作为第一个参数提供给 Server Action

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.
通过将 Server Action 传递给表单的 `action`React 可以 [逐步增强](https://developer.mozilla.org/en-US/docs/Glossary/Progressive_Enhancement) 表单。这意味着表单可以在 JavaScript 捆绑加载之前提交。

#### Handling return values in forms {/*handling-return-values*/}
#### 处理表单中的返回值 {/*handling-return-values*/}

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.
在用户名请求表单中,可能存在用户名不可用的情况。`requestUsername` 应该告诉我们它是否失败。

To update the UI based on the result of a Server Action while supporting progressive enhancement, use [`useFormState`](/reference/react-dom/hooks/useFormState).
请使用 [`useFormState`](/reference/react-dom/hooks/useFormState) 以根据 Server Action 的结果更新 UI 并支持逐步增强。

```js
// requestUsername.js
Expand Down Expand Up @@ -161,21 +161,21 @@ function UsernameForm() {
<>
<form action={action}>
<input type="text" name="username" />
<button type="submit">Request</button>
<button type="submit">请求</button>
</form>
<p>Last submission request returned: {returnValue}</p>
<p>最后一次提交的请求的返回值: {returnValue}</p>
</>
);
}
```

Note that like most Hooks, `useFormState` can only be called in <CodeStep step={1}>[client code](/reference/react/use-client)</CodeStep>.
请注意,与大多数 Hook 一样,`useFormState` 只能在 <CodeStep step={1}>[客户端代码](/reference/react/use-client)</CodeStep> 中调用。

### Calling a Server Action outside of `<form>` {/*calling-a-server-action-outside-of-form*/}
### `<form>` 之外调用 Server Action {/*calling-a-server-action-outside-of-form*/}

Server Actions are exposed server endpoints and can be called anywhere in client code.
Server Action 是暴露的服务器端点,可以在客户端代码的任何位置调用。

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.
在 `<form>` 之外使用 Server Action 时,调用 Server Action 时请使用 [transition](/reference/react/useTransition),这允许显示加载指示器、显示 [乐观状态更新](/reference/react/useOptimistic) 和处理意外错误。表单会自动在 transition 中包装 Server Action。

```js {9-12}
import incrementLike from './actions';
Expand All @@ -194,8 +194,8 @@ function LikeButton() {

return (
<>
<p>Total Likes: {likeCount}</p>
<button onClick={onClick} disabled={isPending}>Like</button>;
<p>点赞数量: {likeCount}</p>
<button onClick={onClick} disabled={isPending}>点赞</button>;
</>
);
}
Expand All @@ -212,4 +212,4 @@ export default async incrementLike() {
}
```

To read a server action return value, you'll need to `await` the promise returned.
可以使用 `await` 获取 Promise 的返回值以读取 Server Action 的返回值。
Loading