diff --git a/src/content/blog/2024/04/25/react-19.md b/src/content/blog/2024/04/25/react-19.md index 5c16ad389d..4487122796 100644 --- a/src/content/blog/2024/04/25/react-19.md +++ b/src/content/blog/2024/04/25/react-19.md @@ -2,39 +2,39 @@ title: "React 19 RC" author: The React Team date: 2024/04/25 -description: React 19 RC is now available on npm! In this post, we'll give an overview of the new features in React 19, and how you can adopt them. +description: React 19 RC 版现在可以在 npm 上使用了! 在这篇文章中,我们将概述 React 19 的新特性,以及如何使用它们。 --- -April 25, 2024 by [The React Team](/community/team) +2024 年 4 月 25 日 [The React Team](/community/team) --- -React 19 RC is now available on npm! +React 19 RC 版本现在可以在 npm 上使用了! -In our [React 19 RC Upgrade Guide](/blog/2024/04/25/react-19-upgrade-guide), we shared step-by-step instructions for upgrading your app to React 19. In this post, we'll give an overview of the new features in React 19, and how you can adopt them. +在我们的 [React 19 RC 升级指南](/blog/2024/04/25/react-19-upgrade-guide) 中, 我们分享了将应用程序升级到 React 19 的分步说明。在这篇文章中,我们将概述 React 19 的新特性,以及如何使用它们。 -- [What's new in React 19](#whats-new-in-react-19) -- [Improvements in React 19](#improvements-in-react-19) -- [How to upgrade](#how-to-upgrade) +- [React 19 中的新功能](#whats-new-in-react-19) +- [React 19 中的改进](#improvements-in-react-19) +- [如何升级](#how-to-upgrade) -For a list of breaking changes, see the [Upgrade Guide](/blog/2024/04/25/react-19-upgrade-guide). +有关破坏性更改的列表,请参阅 [升级指南](/blog/2024/04/25/react-19-upgrade-guide)。 --- -## What's new in React 19 {/*whats-new-in-react-19*/} +## React 19 中的新功能 {/*whats-new-in-react-19*/} ### Actions {/*actions*/} -A common use case in React apps is to perform a data mutation and then update state in response. For example, when a user submits a form to change their name, you will make an API request, and then handle the response. In the past, you would need to handle pending states, errors, optimistic updates, and sequential requests manually. +在 React 应用中,一个常见的用例是执行数据变更,然后响应更新状态。例如,当用户提交一个表单来更改他们的名字,你会发起一个 API 请求,然后处理响应。在过去,你需要手动处理待定状态、错误、乐观更新和顺序请求。 -For example, you could handle the pending and error state in `useState`: +例如,你可以在 `useState` 中处理待定和错误状态: ```js -// Before Actions +// 没有 Actions 之前 function UpdateName({}) { const [name, setName] = useState(""); const [error, setError] = useState(null); @@ -63,12 +63,12 @@ function UpdateName({}) { } ``` -In React 19, we're adding support for using async functions in transitions to handle pending states, errors, forms, and optimistic updates automatically. +在 React 19 中,我们正在添加在过渡中使用异步函数的支持,以自动处理待定状态、错误、表单和乐观更新。 -For example, you can use `useTransition` to handle the pending state for you: +例如,你可以使用 `useTransition` 来为你处理待定状态: ```js -// Using pending state from Actions +// 使用 Actions 中的待定状态 function UpdateName({}) { const [name, setName] = useState(""); const [error, setError] = useState(null); @@ -97,27 +97,27 @@ function UpdateName({}) { } ``` -The async transition will immediately set the `isPending` state to true, make the async request(s), and switch `isPending` to false after any transitions. This allows you to keep the current UI responsive and interactive while the data is changing. +异步过渡会立即将 `isPending` 状态设置为 true,发出异步请求,然后在任何过渡后将 `isPending` 切换为 `false`。这使你能够在数据变化时保持当前 UI 的响应性和交互性。 -#### By convention, functions that use async transitions are called "Actions". {/*by-convention-functions-that-use-async-transitions-are-called-actions*/} +#### 按照惯例,使用异步过渡的函数被称为 "Actions"。 {/*by-convention-functions-that-use-async-transitions-are-called-actions*/} -Actions automatically manage submitting data for you: +Actions 自动为你管理数据提交: -- **Pending state**: Actions provide a pending state that starts at the beginning of a request and automatically resets when the final state update is committed. -- **Optimistic updates**: Actions support the new [`useOptimistic`](#new-hook-optimistic-updates) hook so you can show users instant feedback while the requests are submitting. -- **Error handling**: Actions provide error handling so you can display Error Boundaries when a request fails, and revert optimistic updates to their original value automatically. -- **Forms**: `
` elements now support passing functions to the `action` and `formAction` props. Passing functions to the `action` props use Actions by default and reset the form automatically after submission. +- **待定状态**: Actions 提供一个待定状态,该状态在请求开始时启动,并在最终状态更新提交时自动重置。 +- **乐观更新**: Actions 支持新的 [`useOptimistic`](#new-hook-optimistic-updates) Hook,因此你可以在请求提交时向用户显示即时反馈。 +- **错误处理**: Actions 提供错误处理,因此当请求失败时,你可以显示错误边界,并自动将乐观更新恢复到其原始值。 +- **表单**: `` 元素现在支持将函数传递给 `action` 和 `formAction` 属性。将函数传递给 `action` 属性默认使用 Actions,并在提交后自动重置表单。 -Building on top of Actions, React 19 introduces [`useOptimistic`](#new-hook-optimistic-updates) to manage optimistic updates, and a new hook [`React.useActionState`](#new-hook-useactionstate) to handle common cases for Actions. In `react-dom` we're adding [`` Actions](#form-actions) to manage forms automatically and [`useFormStatus`](#new-hook-useformstatus) to support the common cases for Actions in forms. +在 Actions 的基础上,React 19 引入了 [`useOptimistic`](#new-hook-optimistic-updates) 来管理乐观更新,以及一个新的 Hook [`React.useActionState`](#new-hook-useactionstate) 来处理 Actions 的常见情况。在 `react-dom` 中我们添加了 [`` Actions](#form-actions) 来自动管理表单和 `useFormStatus` 来支持表单中 Actions 的常见情况。 -In React 19, the above example can be simplified to: +在 React 19 中,上述示例可以简化为: ```js -// Using Actions and useActionState +// 使用表单的 Actions 和 useActionState function ChangeName({ name, setName }) { const [error, submitAction, isPending] = useActionState( async (previousState, formData) => { @@ -141,56 +141,56 @@ function ChangeName({ name, setName }) { } ``` -In the next section, we'll break down each of the new Action features in React 19. +在下一节中,我们将详细介绍 React 19 中的每一个新的 Action 功能。 -### New hook: `useActionState` {/*new-hook-useactionstate*/} +### 新的 Hook: `useActionState` {/*new-hook-useactionstate*/} -To make the common cases easier for Actions, we've added a new hook called `useActionState`: +为了使 Actions 的常见情况更加简单,我们添加了一个名为 `useActionState` 的新 Hook: ```js const [error, submitAction, isPending] = useActionState( async (previousState, newName) => { const error = await updateName(newName); if (error) { - // You can return any result of the action. - // Here, we return only the error. + // 您可以返回操作的任何结果。 + // 这里,我们只返回错误。 return error; } - // handle success + // 处理成功的情况。 return null; }, null, ); ``` -`useActionState` accepts a function (the "Action"), and returns a wrapped Action to call. This works because Actions compose. When the wrapped Action is called, `useActionState` will return the last result of the Action as `data`, and the pending state of the Action as `pending`. +`useActionState` 接受一个函数("Action"),并返回一个被包装的用于调用的 Action。这是因为 Actions 是可以组合的。当调用被包装的 Action 时,`useActionState` 将返回 Action 的最后结果作为 `data`,以及 Action 的待定状态作为 `pending`。 -`React.useActionState` was previously called `ReactDOM.useActionState` in the Canary releases, but we've renamed it and deprecated `useActionState`. +`React.useActionState` 在 Canary 版本中曾被称为 `ReactDOM.useActionState`,但我们已经将其重命名并弃用了 `useActionState`。 -See [#28491](https://github.com/facebook/react/pull/28491) for more info. +有关更多信息,请参见 [#28491](https://github.com/facebook/react/pull/28491)。 -For more information, see the docs for [`useActionState`](/reference/react/useActionState). +相关的更多信息,请参阅文档 [`useActionState`](/reference/react/useActionState)。 ### React DOM: `` Actions {/*form-actions*/} -Actions are also integrated with React 19's new `` features for `react-dom`. We've added support for passing functions as the `action` and `formAction` props of ``, ``, and `