Skip to content

Commit b2679bf

Browse files
NewVo1dYucohnyXleine
authored
docs(cn): translate reference/react/useInsertionEffect into Chinese (#1166)
Co-authored-by: Yucohny <[email protected]> Co-authored-by: Xleine <[email protected]>
1 parent e6442a6 commit b2679bf

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

src/content/reference/react/useInsertionEffect.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ title: useInsertionEffect
44

55
<Pitfall>
66

7-
`useInsertionEffect` is for CSS-in-JS library authors. Unless you are working on a CSS-in-JS library and need a place to inject the styles, you probably want [`useEffect`](/reference/react/useEffect) or [`useLayoutEffect`](/reference/react/useLayoutEffect) instead.
7+
`useInsertionEffect` 是为 CSS-in-JS 库的作者特意打造的。除非你正在使用 CSS-in-JS 库并且需要注入样式,否则你应该使用 [`useEffect`](/reference/react/useEffect) 或者 [`useLayoutEffect`](/reference/react/useLayoutEffect)
88

99
</Pitfall>
1010

1111
<Intro>
1212

13-
`useInsertionEffect` is a version of [`useEffect`](/reference/react/useEffect) that fires before any DOM mutations.
13+
`useInsertionEffect` [`useEffect`](/reference/react/useEffect) 的另一种实现,会在任何 DOM 变化前触发。
1414

1515
```js
1616
useInsertionEffect(setup, dependencies?)
@@ -22,80 +22,80 @@ useInsertionEffect(setup, dependencies?)
2222
2323
---
2424
25-
## Reference {/*reference*/}
25+
## 参考 {/*reference*/}
2626
2727
### `useInsertionEffect(setup, dependencies?)` {/*useinsertioneffect*/}
2828
29-
Call `useInsertionEffect` to insert the styles before any DOM mutations:
29+
调用 `useInsertionEffect` 以在任何 DOM 变化之前注入样式:
3030
3131
```js
3232
import { useInsertionEffect } from 'react';
3333

34-
// Inside your CSS-in-JS library
34+
// 在你的 CSS-in-JS 库中
3535
function useCSS(rule) {
3636
useInsertionEffect(() => {
37-
// ... inject <style> tags here ...
37+
// ... 在此注入 <style> 标签 ...
3838
});
3939
return rule;
4040
}
4141
```
4242
43-
[See more examples below.](#usage)
43+
[请参考下方更多示例](#usage)
4444
45-
#### Parameters {/*parameters*/}
45+
#### 参数 {/*parameters*/}
4646
47-
* `setup`: The function with your Effect's logic. Your setup function may also optionally return a *cleanup* function. Before your component is added to the DOM, React will run your setup function. After every re-render with changed dependencies, React will first run the cleanup function (if you provided it) with the old values, and then run your setup function with the new values. Before your component is removed from the DOM, React will run your cleanup function.
48-
49-
* **optional** `dependencies`: The list of all reactive values referenced inside of the `setup` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison algorithm. If you don't specify the dependencies at all, your Effect will re-run after every re-render of the component.
47+
* `setup`:处理 Effect 的函数。setup 函数选择性返回一个 **清理(cleanup)** 函数。在将组件首次添加到 DOM 之前,React 将运行 setup 函数。在每次依赖项变更重新渲染后,如果你提供了 cleanup 函数,那么 React 将首先使用旧值运行该函数,然后使用新值运行 setup 函数。在组件从 DOM 中移除前,React 将运行 cleanup 函数。
5048
51-
#### Returns {/*returns*/}
49+
* **可选** `dependencies``setup` 代码中引用的所有响应式值的列表。响应式值包括 props、state 以及所有直接在组件内部声明的变量和函数。如果你的代码检查工具 [配置了 React](/learn/editor-setup#linting),那么它将验证是否每个响应式值都被正确地指定为依赖项。依赖列表必须具有固定数量的项,并且必须像 `[dep1, dep2, dep3]` 这样内联编写。React 将使用 [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) 来比较每个依赖项和它先前的值。如果省略此参数,则将在每次重新渲染组件之后重新运行 Effect。
5250
53-
`useInsertionEffect` returns `undefined`.
51+
#### 返回值 {/*returns*/}
5452
55-
#### Caveats {/*caveats*/}
53+
`useInsertionEffect` 返回 `undefined`
5654
57-
* Effects only run on the client. They don't run during server rendering.
58-
* You can't update state from inside `useInsertionEffect`.
59-
* By the time `useInsertionEffect` runs, refs are not attached yet, and DOM is not yet updated.
55+
#### 注意事项 {/*caveats*/}
56+
57+
* Effect 只在客户端上运行,在服务端渲染中不会运行。
58+
* 避免在 `useInsertionEffect` 内部更新 state。
59+
* 在 `useInsertionEffect` 运行期间,ref 还没有附加,DOM 也还没有更新。
6060
6161
---
6262
63-
## Usage {/*usage*/}
63+
## 用法 {/*usage*/}
6464
65-
### Injecting dynamic styles from CSS-in-JS libraries {/*injecting-dynamic-styles-from-css-in-js-libraries*/}
65+
### CSS-in-JS 库中注入动态样式 {/*injecting-dynamic-styles-from-css-in-js-libraries*/}
6666
67-
Traditionally, you would style React components using plain CSS.
67+
传统上,你会使用纯 CSS 为 React 组件设置样式。
6868
6969
```js
70-
// In your JS file:
70+
// 在你的 JS 文件中:
7171
<button className="success" />
7272

73-
// In your CSS file:
73+
// 在你的 CSS 文件中:
7474
.success { color: green; }
7575
```
7676
77-
Some teams prefer to author styles directly in JavaScript code instead of writing CSS files. This usually requires using a CSS-in-JS library or a tool. There are three common approaches to CSS-in-JS:
77+
有些团队更喜欢直接在 JavaScript 代码中编写样式,而不是编写 CSS 文件。这通常需要使用 CSS-in-JS 库或工具。以下是 CSS-in-JS 三种常见的实现方法:
7878
79-
1. Static extraction to CSS files with a compiler
80-
2. Inline styles, e.g. `<div style={{ opacity: 1 }}>`
81-
3. Runtime injection of `<style>` tags
79+
1. 使用编译器静态提取到 CSS 文件
80+
2. 内联样式,例如 `<div style={{ opacity: 1 }}>`
81+
3. 运行时注入 `<style>` 标签
8282
83-
If you use CSS-in-JS, we recommend a combination of the first two approaches (CSS files for static styles, inline styles for dynamic styles). **We don't recommend runtime `<style>` tag injection for two reasons:**
83+
如果你使用 CSS-in-JS,我们建议结合使用前两种方法(静态样式使用 CSS 文件,动态样式使用内联样式)。**我们不建议运行时注入 `<style>` 标签有两个原因**:
8484
85-
1. Runtime injection forces the browser to recalculate the styles a lot more often.
86-
2. Runtime injection can be very slow if it happens at the wrong time in the React lifecycle.
85+
1. 运行时注入会使浏览器频繁地重新计算样式。
86+
2. 如果在 React 生命周期中某个错误的时机进行运行时注入,它可能会非常慢。
8787
88-
The first problem is not solvable, but `useInsertionEffect` helps you solve the second problem.
88+
第一个问题无法解决,但是 `useInsertionEffect` 可以帮助你解决第二个问题。
8989
90-
Call `useInsertionEffect` to insert the styles before any DOM mutations:
90+
调用 `useInsertionEffect` 以在任何 DOM 变化之前注入样式:
9191
9292
```js {4-11}
93-
// Inside your CSS-in-JS library
93+
// 在你的 CSS-in-JS 库中
9494
let isInserted = new Set();
9595
function useCSS(rule) {
9696
useInsertionEffect(() => {
97-
// As explained earlier, we don't recommend runtime injection of <style> tags.
98-
// But if you have to do it, then it's important to do in useInsertionEffect.
97+
// 同前所述,我们不建议在运行时注入 <style> 标签。
98+
// 如果你必须这样做,那么应当在 useInsertionEffect 中进行。
9999
if (!isInserted.has(rule)) {
100100
isInserted.add(rule);
101101
document.head.appendChild(getStyleForRule(rule));
@@ -110,7 +110,7 @@ function Button() {
110110
}
111111
```
112112
113-
Similarly to `useEffect`, `useInsertionEffect` does not run on the server. If you need to collect which CSS rules have been used on the server, you can do it during rendering:
113+
`useEffect` 类似,`useInsertionEffect` 不在服务端运行。如果你需要收集在服务端上使用了哪些 CSS 规则,你可以在渲染期间进行:
114114
115115
```js {1,4-6}
116116
let collectedRulesSet = new Set();
@@ -126,14 +126,14 @@ function useCSS(rule) {
126126
}
127127
```
128128
129-
[Read more about upgrading CSS-in-JS libraries with runtime injection to `useInsertionEffect`.](https://github.com/reactwg/react-18/discussions/110)
129+
[阅读更多使用 `useInsertionEffect` 升级 CSS-in-JS 库的相关指南](https://github.com/reactwg/react-18/discussions/110)
130130
131131
<DeepDive>
132132
133-
#### How is this better than injecting styles during rendering or useLayoutEffect? {/*how-is-this-better-than-injecting-styles-during-rendering-or-uselayouteffect*/}
133+
#### 这与在渲染期间或 `useLayoutEffect` 中注入样式相比有何优势? {/*how-is-this-better-than-injecting-styles-during-rendering-or-uselayouteffect*/}
134134
135-
If you insert styles during rendering and React is processing a [non-blocking update,](/reference/react/useTransition#marking-a-state-update-as-a-non-blocking-transition) the browser will recalculate the styles every single frame while rendering a component tree, which can be **extremely slow.**
135+
如果你在渲染期间注入样式并且 React 正在处理 [非阻塞更新](/reference/react/useTransition#marking-a-state-update-as-a-non-blocking-transition),那么浏览器将在渲染组件树时每一帧都会重新计算样式,这可能会 **非常慢**。
136136
137-
`useInsertionEffect` is better than inserting styles during [`useLayoutEffect`](/reference/react/useLayoutEffect) or [`useEffect`](/reference/react/useEffect) because it ensures that by the time other Effects run in your components, the `<style>` tags have already been inserted. Otherwise, layout calculations in regular Effects would be wrong due to outdated styles.
137+
`useInsertionEffect` 比在 [`useLayoutEffect`](/reference/react/useLayoutEffect) [`useEffect`](/reference/react/useEffect) 期间注入样式更好。因为它会确保 `<style>` 标签在其它 Effect 运行前被注入。否则,正常的 Effect 中的布局计算将由于过时的样式而出错。
138138
139139
</DeepDive>

0 commit comments

Comments
 (0)