From f063b40ae570e76df3b196e19f194cfc04506214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kiner-tang=28=E6=96=87=E8=BE=89=29?= <1127031143@qq.com> Date: Sun, 19 May 2024 11:11:36 +0800 Subject: [PATCH 01/16] docs(cn): translate blog/2024/04/25/react-19 into Chinese --- src/content/blog/2024/04/25/react-19.md | 269 ++++++++++++------------ 1 file changed, 134 insertions(+), 135 deletions(-) diff --git a/src/content/blog/2024/04/25/react-19.md b/src/content/blog/2024/04/25/react-19.md index 8224cfe9e0..033c55409a 100644 --- a/src/content/blog/2024/04/25/react-19.md +++ b/src/content/blog/2024/04/25/react-19.md @@ -2,42 +2,42 @@ title: "React 19 Beta" author: The React Team date: 2024/04/25 -description: React 19 Beta 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 测试版现在可以在 npm 上使用了! 在这篇文章中,我们将概述 React 19 的新特性,以及如何使用它们。 --- -April 25, 2024 by [The React Team](/community/team) +2023 年 4 月 25 日 [The React Team](/community/team) --- -This beta release is for libraries to prepare for React 19. App developers should upgrade to 18.3.0 and wait for React 19 stable as we work with libraries and make changes based on feedback. +这个测试版的发布是为了 React 19 的库做准备。应用开发者应该升级到 18.3.0,并等待 React 19 稳定版发布,因为我们正在维护这个库,并根据反馈进行更改。 -React 19 Beta is now available on npm! +React 19 测试版现在可以在 npm 上使用了! -In our [React 19 Beta Upgrade Guide](/blog/2024/04/25/react-19-upgrade-guide), we shared step-by-step instructions for upgrading your app to React 19 Beta. In this post, we'll give an overview of the new features in React 19, and how you can adopt them. +在我们的 [React 19 Beta 升级指南](/blog/2024/04/25/react-19-upgrade-guide) 中,我们分享了将您的应用升级到 React 19 Beta 的逐步指南。在这篇文章中,我们将概述 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 @@ -69,12 +69,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); @@ -103,24 +103,24 @@ 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) 钩子,因此你可以在请求提交时向用户显示即时反馈。 +- **错误处理**: 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) 来管理乐观更新,以及一个新的钩子 [`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 @@ -147,11 +147,11 @@ 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*/} +### 新的钩子: `useActionState` {/*new-hook-useactionstate*/} -To make the common cases easier for Actions, we've added a new hook called `useActionState`: +为了使 Actions 的常见情况更加简单,我们添加了一个名为 `useActionState` 的新钩子: ```js const [error, submitAction, isPending] = useActionState( @@ -170,33 +170,33 @@ const [error, submitAction, isPending] = useActionState( ); ``` -`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 `