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/useInsertionEffect into Chinese #1166

Merged
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
e193b95
docs(cn): translate reference/react/useInsertionEffect into Chinese
NewVo1d May 11, 2023
260b1ce
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 21, 2023
b27848a
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 21, 2023
8e5641c
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 21, 2023
771dfdb
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 21, 2023
8661373
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 21, 2023
847c217
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 21, 2023
f49d8d0
Merge branch 'main' into translate/reference/react/useInsertionEffect
NewVo1d May 21, 2023
6140c63
edited inappropriate part
NewVo1d May 21, 2023
30b76c3
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 30, 2023
df0a49a
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 30, 2023
15b24ef
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 30, 2023
9267df5
Update src/content/reference/react/useInsertionEffect.md
NewVo1d May 30, 2023
ec6894c
Update src/content/reference/react/useInsertionEffect.md
NewVo1d Jun 2, 2023
4ef9216
Update src/content/reference/react/useInsertionEffect.md
NewVo1d Jun 2, 2023
a588cef
Update src/content/reference/react/useInsertionEffect.md
NewVo1d Jun 2, 2023
f35b08e
Update src/content/reference/react/useInsertionEffect.md
NewVo1d Jun 2, 2023
a85bc25
Update src/content/reference/react/useInsertionEffect.md
Yucohny Jun 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions src/content/reference/react/useInsertionEffect.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ title: useInsertionEffect

<Pitfall>

`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.
`useInsertionEffect` 是为 CSS-in-JS 库的作者特意打造的。除非你正在使用 CSS-in-JS 库并且需要注入样式,否则你应该使用 [`useEffect`](/reference/react/useEffect) 或者 [`useLayoutEffect`](/reference/react/useLayoutEffect)

</Pitfall>

<Intro>

`useInsertionEffect` is a version of [`useEffect`](/reference/react/useEffect) that fires before any DOM mutations.
`useInsertionEffect` [`useEffect`](/reference/react/useEffect) 的另一种实现,在任何 DOM 变化前触发。
NewVo1d marked this conversation as resolved.
Show resolved Hide resolved

```js
useInsertionEffect(setup, dependencies?)
Expand All @@ -22,80 +22,80 @@ useInsertionEffect(setup, dependencies?)

---

## Reference {/*reference*/}
## 参考 {/*reference*/}

### `useInsertionEffect(setup, dependencies?)` {/*useinsertioneffect*/}

Call `useInsertionEffect` to insert the styles before any DOM mutations:
调用 `useInsertionEffect` 以在任何 DOM 变化之前注入样式:

```js
import { useInsertionEffect } from 'react';

// Inside your CSS-in-JS library
// 在你的 CSS-in-JS 库中
function useCSS(rule) {
useInsertionEffect(() => {
// ... inject <style> tags here ...
// ... 在此注入 <style> 标签 ...
});
return rule;
}
```

[See more examples below.](#usage)
[请参考下方更多示例](#usage)

#### Parameters {/*parameters*/}
#### 参数 {/*parameters*/}

* `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.

* **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.
* `setup`:处理 Effect 的函数。setup 函数选择性返回一个 **清理(cleanup)** 函数。在将组件首次添加到 DOM 之前,React 将运行 setup 函数。在每次依赖项变更重新渲染后,如果你提供了 cleanup 函数,那么 React 将首先使用旧值运行该函数,然后使用新值运行 setup 函数。在组件从 DOM 中移除前,React 将运行 cleanup 函数。

#### Returns {/*returns*/}
* **可选** `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。
NewVo1d marked this conversation as resolved.
Show resolved Hide resolved

`useInsertionEffect` returns `undefined`.
#### 返回值 {/*returns*/}

#### Caveats {/*caveats*/}
`useInsertionEffect` 返回 `undefined`。

* Effects only run on the client. They don't run during server rendering.
* You can't update state from inside `useInsertionEffect`.
* By the time `useInsertionEffect` runs, refs are not attached yet, and DOM is not yet updated.
#### 注意事项 {/*caveats*/}

* Effect 只在客户端上运行,在服务端渲染中不会运行。
* 你不能在 `useInsertionEffect` 内部更新 state。
NewVo1d marked this conversation as resolved.
Show resolved Hide resolved
* 在 `useInsertionEffect` 运行期间,refs 还没有附加,DOM 也还没有更新。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*`useInsertionEffect` 运行期间,refs 还没有附加,DOM 也还没有更新。
*`useInsertionEffect` 运行期间,ref 还没有附加,DOM 也还没有更新。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“附加”这个表达感觉有点点怪,有没有其他更合适的表达方式?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当初翻译的时候,考虑如下:

  • ref 如果用于计算值的时候,使用 赋值 比较合适;
  • ref 如果用于 DOM 的时候,使用 赋值 感觉差点意思,故使用了 attach 的直译 附加

暂未想到对 attach 更好的表达方式,但是 赋值 并无不妥之处。如同意,我会将其改为 赋值

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以考虑加个因果关系?类似于:因为 DOM 还没有更新,所以 ref 还没有被赋值。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以考虑加个因果关系?类似于:因为 DOM 还没有更新,所以 ref 还没有被赋值。

这个因果关系是确认的吗?


---

## Usage {/*usage*/}
## 用法 {/*usage*/}

### Injecting dynamic styles from CSS-in-JS libraries {/*injecting-dynamic-styles-from-css-in-js-libraries*/}
### CSS-in-JS 库中注入动态样式 {/*injecting-dynamic-styles-from-css-in-js-libraries*/}

Traditionally, you would style React components using plain CSS.
传统上,你会使用纯 CSS 为 React 组件设置样式。

```js
// In your JS file:
// 在你的 JS 文件中:
<button className="success" />

// In your CSS file:
// 在你的 CSS 文件中:
.success { color: green; }
```

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:
有些团队更喜欢直接在 JavaScript 代码中编写样式,而不是编写 CSS 文件。这通常需要使用 CSS-in-JS 库或工具。以下是 CSS-in-JS 三种常见的实现方法:

1. Static extraction to CSS files with a compiler
2. Inline styles, e.g. `<div style={{ opacity: 1 }}>`
3. Runtime injection of `<style>` tags
1. 使用编译器静态提取到 CSS 文件
2. 内联样式,例如 `<div style={{ opacity: 1 }}>`
3. 运行时注入 `<style>` 标签

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:**
如果你使用 CSS-in-JS,我们建议结合使用前两种方法(静态样式使用 CSS 文件,动态样式使用内联样式)。**我们不建议运行时注入 `<style>` 标签有两个原因**:

1. Runtime injection forces the browser to recalculate the styles a lot more often.
2. Runtime injection can be very slow if it happens at the wrong time in the React lifecycle.
1. 运行时注入会使浏览器频繁地重新计算样式。
2. 如果运行时注入发生在 React 生命周期的错误时间,它可能会非常慢。
NewVo1d marked this conversation as resolved.
Show resolved Hide resolved

The first problem is not solvable, but `useInsertionEffect` helps you solve the second problem.
第一个问题无法解决,但是 `useInsertionEffect` 可以帮助你解决第二个问题。

Call `useInsertionEffect` to insert the styles before any DOM mutations:
调用 `useInsertionEffect` 以在任何 DOM 变化之前注入样式:

```js {4-11}
// Inside your CSS-in-JS library
// 在你的 CSS-in-JS 库中
let isInserted = new Set();
function useCSS(rule) {
useInsertionEffect(() => {
// As explained earlier, we don't recommend runtime injection of <style> tags.
// But if you have to do it, then it's important to do in useInsertionEffect.
// 同前所述,我们不建议在运行时注入 <style> 标签。
// 如果你必须这样做,那么应当在 useInsertionEffect 中进行。
if (!isInserted.has(rule)) {
isInserted.add(rule);
document.head.appendChild(getStyleForRule(rule));
Expand All @@ -110,7 +110,7 @@ function Button() {
}
```

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:
`useEffect` 类似,`useInsertionEffect` 不在服务端运行。如果你需要收集在服务端上使用了哪些 CSS 规则,你可以在渲染期间进行:

```js {1,4-6}
let collectedRulesSet = new Set();
Expand All @@ -126,14 +126,14 @@ function useCSS(rule) {
}
```

[Read more about upgrading CSS-in-JS libraries with runtime injection to `useInsertionEffect`.](https://github.com/reactwg/react-18/discussions/110)
[阅读更多将 `useInsertionEffect` 应用于带有运行时注入的 CSS-in-JS 库的相关信息](https://github.com/reactwg/react-18/discussions/110)
NewVo1d marked this conversation as resolved.
Show resolved Hide resolved

<DeepDive>

#### How is this better than injecting styles during rendering or useLayoutEffect? {/*how-is-this-better-than-injecting-styles-during-rendering-or-uselayouteffect*/}
#### 这与渲染期间或 `useLayoutEffect` 中注入样式相比有何优势? {/*how-is-this-better-than-injecting-styles-during-rendering-or-uselayouteffect*/}
NewVo1d marked this conversation as resolved.
Show resolved Hide resolved

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.**
如果你在渲染期间注入样式并且 React 正在处理 [非阻塞更新](/reference/react/useTransition#marking-a-state-update-as-a-non-blocking-transition),那么浏览器将在渲染组件树时每一帧都会重新计算样式,这可能会 **非常慢**。

`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.
`useInsertionEffect` 比在 [`useLayoutEffect`](/reference/react/useLayoutEffect) [`useEffect`](/reference/react/useEffect) 期间注入样式更好。因为它确保其他 Effect 在你的组件中运行时,`<style>` 标签已经被注入。否则,正常的 Effect 中的布局计算将由于过时的样式而出错。
Yucohny marked this conversation as resolved.
Show resolved Hide resolved

</DeepDive>