Skip to content

Commit dd66864

Browse files
authored
docs(cn): translate react-compiler/introduction into Chinese (#1742)
2 parents 6c1e178 + 22789ec commit dd66864

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/content/learn/react-compiler/introduction.md

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ title: Introduction
33
---
44

55
<Intro>
6-
React Compiler is a new build-time tool that automatically optimizes your React app. It works with plain JavaScript, and understands the [Rules of React](/reference/rules), so you don't need to rewrite any code to use it.
6+
React Compiler 是一个新的构建时工具,它可以自动优化你的 React 应用。它支持纯 JavaScript,并且了解 [React 的规则](/reference/rules),因此你无需重写任何代码即可使用它。
77
</Intro>
88

99
<YouWillLearn>
1010

11-
* What React Compiler does
12-
* Getting started with the compiler
13-
* Incremental adoption strategies
14-
* Debugging and troubleshooting when things go wrong
15-
* Using the compiler on your React library
11+
* React 编译器的作用
12+
* 如何开始使用该编译器
13+
* 渐进式采用策略
14+
* 出现问题时的调试与排查
15+
* 在你的 React 库中使用该编译器
1616

1717
</YouWillLearn>
1818

1919
<Note>
20-
React Compiler is currently in Release Candidate (RC). We now recommend everyone to try the compiler and provide feedback. The latest RC release can be found with the `@rc` tag.
20+
React Compiler 目前处于发布候选(RC)阶段。我们现在建议所有人尝试使用该编译器并提供反馈。最新的 RC 版本可以通过 `@rc` 标签找到。
2121
</Note>
2222

23-
## What does React Compiler do? {/*what-does-react-compiler-do*/}
23+
## React 编译器是做什么的? {/*what-does-react-compiler-do*/}
2424

25-
React Compiler automatically optimizes your React application at build time. React is often fast enough without optimization, but sometimes you need to manually memoize components and values to keep your app responsive. This manual memoization is tedious, easy to get wrong, and adds extra code to maintain. React Compiler does this optimization automatically for you, freeing you from this mental burden so you can focus on building features.
25+
React Compiler 会在构建时自动优化你的 React 应用。通常情况下,即使不进行优化,React 的性能也已经足够快,但有时你需要手动对组件和值进行记忆化(memoization)以保持应用的响应速度。这种手动记忆化既繁琐又容易出错,并且会增加需要维护的额外代码。React Compiler 为你自动完成这些优化,减轻了你的思维负担,使你可以专注于功能的开发。
2626

27-
### Before React Compiler {/*before-react-compiler*/}
27+
### 在使用 React 编译器之前 {/*before-react-compiler*/}
2828

29-
Without the compiler, you need to manually memoize components and values to optimize re-renders:
29+
没有编译器的情况下,你需要手动对组件和值进行记忆化以优化重新渲染:
3030

3131
```js
3232
import { useMemo, useCallback, memo } from 'react';
@@ -50,9 +50,9 @@ const ExpensiveComponent = memo(function ExpensiveComponent({ data, onClick }) {
5050
});
5151
```
5252

53-
### After React Compiler {/*after-react-compiler*/}
53+
### 在使用 React 编译器之后 {/*after-react-compiler*/}
5454

55-
With React Compiler, you write the same code without manual memoization:
55+
使用 React 编译器,你可以编写相同的代码而无需手动进行记忆化:
5656

5757
```js
5858
function ExpensiveComponent({ data, onClick }) {
@@ -72,23 +72,23 @@ function ExpensiveComponent({ data, onClick }) {
7272
}
7373
```
7474

75-
_[See this example in the React Compiler Playground](https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEAogB4AOCmYeAbggMIQC2Fh1OAFMEQCYBDHAIA0RQowA2eOAGsiAXwCURYAB1iROITA4iFGBERgwCPgBEhAogF4iCStVoMACoeO1MAcy6DhSgG4NDSItHT0ACwFMPkkmaTlbIi48HAQWFRsAPlUQ0PFMKRlZFLSWADo8PkC8hSDMPJgEHFhiLjzQgB4+eiyO-OADIwQTM0thcpYBClL02xz2zXz8zoBJMqJZBABPG2BU9Mq+BQKiuT2uTJyomLizkoOMk4B6PqX8pSUFfs7nnro3qEapgFCAFEA)_
75+
__[React 编译器游乐场中查看此示例](https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEAogB4AOCmYeAbggMIQC2Fh1OAFMEQCYBDHAIA0RQowA2eOAGsiAXwCURYAB1iROITA4iFGBERgwCPgBEhAogF4iCStVoMACoeO1MAcy6DhSgG4NDSItHT0ACwFMPkkmaTlbIi48HAQWFRsAPlUQ0PFMKRlZFLSWADo8PkC8hSDMPJgEHFhiLjzQgB4+eiyO-OADIwQTM0thcpYBClL02xz2zXz8zoBJMqJZBABPG2BU9Mq+BQKiuT2uTJyomLizkoOMk4B6PqX8pSUFfs7nnro3qEapgFCAFEA)__
7676

77-
React Compiler automatically applies the equivalent optimizations, ensuring your app only re-renders when necessary.
77+
React 编译器会自动应用等效的优化,确保你的应用只在必要时重新渲染。
7878

7979
<DeepDive>
80-
#### What kind of memoization does React Compiler add? {/*what-kind-of-memoization-does-react-compiler-add*/}
80+
#### React 编译器添加了哪种类型的记忆化? {/*what-kind-of-memoization-does-react-compiler-add*/}
8181

82-
React Compiler's automatic memoization is primarily focused on **improving update performance** (re-rendering existing components), so it focuses on these two use cases:
82+
React 编译器的自动记忆化主要专注于 **提高更新性能(重新渲染现有组件)**,因此它聚焦于以下两种使用场景:
8383

84-
1. **Skipping cascading re-rendering of components**
85-
* Re-rendering `<Parent />` causes many components in its component tree to re-render, even though only `<Parent />` has changed
86-
1. **Skipping expensive calculations from outside of React**
87-
* For example, calling `expensivelyProcessAReallyLargeArrayOfObjects()` inside of your component or hook that needs that data
84+
1. **跳过组件的级联重新渲染**
85+
* 重新渲染 `<Parent />` 会导致其组件树中的许多组件重新渲染,即使只有 `<Parent />` 发生了变化
86+
1. **跳过 React 外部的昂贵计算**
87+
* 例如,在组件或 Hook 中调用 `expensivelyProcessAReallyLargeArrayOfObjects()` 这个处理大数据数组的函数
8888

89-
#### Optimizing Re-renders {/*optimizing-re-renders*/}
89+
#### 优化重新渲染 {/*optimizing-re-renders*/}
9090

91-
React lets you express your UI as a function of their current state (more concretely: their props, state, and context). In its current implementation, when a component's state changes, React will re-render that component _and all of its children_ — unless you have applied some form of manual memoization with `useMemo()`, `useCallback()`, or `React.memo()`. For example, in the following example, `<MessageButton>` will re-render whenever `<FriendList>`'s state changes:
91+
React 允许你将 UI 表达为其当前状态的函数(更具体地说:其 propsstate context)。在其当前的实现中,当一个组件的状态发生变化时,React 会重新渲染该组件 __及其所有子组件__,除非你使用了 `useMemo()``useCallback()` `React.memo()` 进行手动记忆优化。例如,在以下示例中,每当 `<FriendList>` 的状态变化时,`<MessageButton>` 都会重新渲染:
9292

9393
```javascript
9494
function FriendList({ friends }) {
@@ -136,41 +136,41 @@ However, if `expensivelyProcessAReallyLargeArrayOfObjects` is truly an expensive
136136
So if `expensivelyProcessAReallyLargeArrayOfObjects` was used in many different components, even if the same exact items were passed down, that expensive calculation would be run repeatedly. We recommend [profiling](reference/react/useMemo#how-to-tell-if-a-calculation-is-expensive) first to see if it really is that expensive before making code more complicated.
137137
</DeepDive>
138138

139-
## Should I try out the compiler? {/*should-i-try-out-the-compiler*/}
139+
## 我应该尝试这个编译器吗? {/*should-i-try-out-the-compiler*/}
140140

141-
We encourage everyone to start using React Compiler. While the compiler is still an optional addition to React today, in the future some features may require the compiler in order to fully work.
141+
我们鼓励大家开始使用 React 编译器。虽然目前编译器仍是 React 的可选附加功能,但未来某些功能可能需要编译器才能完整运行。
142142

143-
### Is it safe to use? {/*is-it-safe-to-use*/}
143+
### 使用起来安全吗? {/*is-it-safe-to-use*/}
144144

145-
React Compiler is now in RC and has been tested extensively in production. While it has been used in production at companies like Meta, rolling out the compiler to production for your app will depend on the health of your codebase and how well you've followed the [Rules of React](/reference/rules).
145+
React 编译器目前已进入 RC 阶段,并已在生产环境中进行了广泛测试。虽然像 Meta 这样的公司已经在生产中使用了它,但是否将编译器部署到你的应用程序生产环境,将取决于你的代码库状况以及你对 [React 规则](/reference/rules) 的遵循程度。
146146

147-
## What build tools are supported? {/*what-build-tools-are-supported*/}
147+
## 支持哪些构建工具? {/*what-build-tools-are-supported*/}
148148

149-
React Compiler can be installed across [several build tools](/learn/react-compiler/installation) such as Babel, Vite, Metro, and Rsbuild.
149+
React Compiler 可以在多个构建工具中安装,例如 [Babel、Vite、Metro 和 Rsbuild](/learn/react-compiler/installation)
150150

151-
React Compiler is primarily a light Babel plugin wrapper around the core compiler, which was designed to be decoupled from Babel itself. While the initial stable version of the compiler will remain primarily a Babel plugin, we are working with the swc and [oxc](https://github.com/oxc-project/oxc/issues/10048) teams to build first class support for React Compiler so you won't have to add Babel back to your build pipelines in the future.
151+
React Compiler 主要是围绕核心编译器构建的一个轻量级 Babel 插件封装,其设计初衷是为了与 Babel 本身解耦。尽管编译器的第一个稳定版本主要仍然是一个 Babel 插件,但我们正在与 swc [oxc](https://github.com/oxc-project/oxc/issues/10048) 团队合作,为 React Compiler 构建一流的支持,这样你将来无需再将 Babel 添加到你的构建流程中。
152152

153-
Next.js users can enable the swc-invoked React Compiler by using [v15.3.1](https://github.com/vercel/next.js/releases/tag/v15.3.1) and up.
153+
Next.js 用户可以通过使用 [v15.3.1](https://github.com/vercel/next.js/releases/tag/v15.3.1) 或更高版本来启用由 swc 调用的 React Compiler。
154154

155-
## What should I do about useMemo, useCallback, and React.memo? {/*what-should-i-do-about-usememo-usecallback-and-reactmemo*/}
155+
## 关于 useMemouseCallback React.memo 我应该怎么做? {/*what-should-i-do-about-usememo-usecallback-and-reactmemo*/}
156156

157-
If you are using React Compiler, [`useMemo`](/reference/react/useMemo), [`useCallback`](/reference/react/useCallback), and [`React.memo`](/reference/react/memo) can be removed. React Compiler adds automatic memoization more precisely and granularly than is possible with these hooks. If you choose to keep manual memoization, React Compiler will analyze them and determine if your manual memoization matches its automatically inferred memoization. If there isn't a match, the compiler will choose to bail out of optimizing that component.
157+
如果你正在使用 React 编译器,可以移除 [`useMemo`](/reference/react/useMemo)[`useCallback`](/reference/react/useCallback) [`React.memo`](/reference/react/memo)React 编译器能够比使用这些 Hook 更精确和细致地添加自动记忆化功能。如果你选择保留手动记忆化,React 编译器会分析它们,并判断你的手动记忆化是否与其自动推断出的记忆化一致。如果不一致,编译器将选择放弃优化该组件。
158158

159-
This is done out of caution as a common anti-pattern with manual memoization is using it for correctness. This means your app depends on specific values being memoized to work properly. For example, in order to prevent an infinite loop, you may have memoized some values to stop a `useEffect` call from firing. This breaks the Rules of React, but since it can potentially be dangerous for the compiler to automatically remove manual memoization, the compiler will just bail out instead. You should manually remove your handwritten memoization and verify that your app still works as expected.
159+
这样做是出于谨慎考虑,因为手动记忆化常见的反模式是为了保证程序的正确性。这意味着你的应用依赖于对特定值进行记忆化才能正常运行。例如,为了防止无限循环,你可能会记忆某些值来阻止 `useEffect` 被触发。这违反了 React 的规则,但因为编译器自动移除手动记忆化可能会有潜在危险,所以会直接放弃优化。你应该手动移除自己的手动记忆化代码,并验证应用是否仍能按预期运行。
160160

161-
## Try React Compiler {/*try-react-compiler*/}
161+
## 尝试 React 编译器 {/*try-react-compiler*/}
162162

163-
This section will help you get started with React Compiler and understand how to use it effectively in your projects.
163+
本节将帮助你开始使用 React 编译器,并了解如何在项目中有效地使用它。
164164

165-
* **[Installation](/learn/react-compiler/installation)** - Install React Compiler and configure it for your build tools
166-
* **[React Version Compatibility](/reference/react-compiler/target)** - Support for React 17, 18, and 19
167-
* **[Configuration](/reference/react-compiler/configuration)** - Customize the compiler for your specific needs
168-
* **[Incremental Adoption](/learn/react-compiler/incremental-adoption)** - Strategies for gradually rolling out the compiler in existing codebases
169-
* **[Debugging and Troubleshooting](/learn/react-compiler/debugging)** - Identify and fix issues when using the compiler
170-
* **[Compiling Libraries](/reference/react-compiler/compiling-libraries)** - Best practices for shipping compiled code
171-
* **[API Reference](/reference/react-compiler/configuration)** - Detailed documentation of all configuration options
165+
* **[安装](/learn/react-compiler/installation)** - 安装 React 编译器并为你的构建工具进行配置
166+
* **[React 版本兼容性](/reference/react-compiler/target)** - 支持 React 17、18 和 19
167+
* **[配置](/reference/react-compiler/configuration)** - 根据你的特定需求自定义编译器
168+
* **[渐进式采用](/learn/react-compiler/incremental-adoption)** - 在现有代码库中逐步推广编译器的策略
169+
* **[调试与故障排除](/learn/react-compiler/debugging)** - 识别并解决使用编译器时的问题
170+
* **[编译库](/reference/react-compiler/compiling-libraries)** - 发布已编译代码的最佳实践
171+
* **[API 参考](/reference/react-compiler/configuration)** - 所有配置选项的详细文档
172172

173-
## Additional resources {/*additional-resources*/}
173+
## 其他资源 {/*additional-resources*/}
174174

175-
In addition to these docs, we recommend checking the [React Compiler Working Group](https://github.com/reactwg/react-compiler) for additional information and discussion about the compiler.
175+
除了这些文档外,我们还建议查看 [React 编译器工作组](https://github.com/reactwg/react-compiler) 以获取有关该编译器的更多信息和讨论。
176176

0 commit comments

Comments
 (0)