Skip to content

Commit 72d2b79

Browse files
authored
Merge pull request #625 from reactjs/tr/useInsertionEffect
Translate "useInsertionEffect"
2 parents 0d0aba6 + e914c2b commit 72d2b79

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/content/reference/react/useInsertionEffect.md

+33-33
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` allows inserting elements into the DOM before any layout effects fire.
13+
`useInsertionEffect` はレイアウトエフェクトが発火する前に DOM に要素を挿入するために使用します。
1414

1515
```js
1616
useInsertionEffect(setup, dependencies?)
@@ -22,11 +22,11 @@ useInsertionEffect(setup, dependencies?)
2222
2323
---
2424
25-
## Reference {/*reference*/}
25+
## リファレンス {/*reference*/}
2626
2727
### `useInsertionEffect(setup, dependencies?)` {/*useinsertioneffect*/}
2828
29-
Call `useInsertionEffect` to insert styles before any effects fire that may need to read layout:
29+
`useInsertionEffect` を呼び出して、レイアウトを読み取る可能性があるエフェクトが実行される前にスタイルの挿入を行います。
3030
3131
```js
3232
import { useInsertionEffect } from 'react';
@@ -40,32 +40,32 @@ function useCSS(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. When your component is added to the DOM, but before any layout effects fire, 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. When your component is removed from the DOM, React will run your cleanup function.
47+
* `setup`: エフェクトのロジックが記述された関数です。このセットアップ関数は、オプションで*クリーンアップ*関数を返すことができます。コンポーネントが初めて DOM に追加された後、レイアウトエフェクトが発火する前に、React はセットアップ関数を実行します。依存配列 (dependencies) が変更された再レンダー時には、React はまず古い値を使ってクリーンアップ関数(あれば)を実行し、次に新しい値を使ってセットアップ関数を実行します。コンポーネントが DOM から削除された後、React はクリーンアップ関数を最後にもう一度実行します。
4848
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.
49+
* **省略可能** `dependencies`: `setup` コード内で参照されるすべてのリアクティブな値のリストです。リアクティブな値には、propsstate、コンポーネント本体に直接宣言されたすべての変数および関数が含まれます。リンタが [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) を使った比較で、それぞれの依存値を以前の値と比較します。この引数を省略すると、エフェクトはコンポーネントの毎回のレンダー後に再実行されます。
5050
51-
#### Returns {/*returns*/}
51+
#### 返り値 {/*returns*/}
5252
53-
`useInsertionEffect` returns `undefined`.
53+
`useInsertionEffect` `undefined` を返します。
5454
55-
#### Caveats {/*caveats*/}
55+
#### 注意点 {/*caveats*/}
5656
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.
60-
* `useInsertionEffect` may run either before or after the DOM has been updated. You shouldn't rely on the DOM being updated at any particular time.
61-
* Unlike other types of Effects, which fire cleanup for every Effect and then setup for every Effect, `useInsertionEffect` will fire both cleanup and setup one component at a time. This results in an "interleaving" of the cleanup and setup functions.
57+
* エフェクトはクライアント上でのみ実行されます。サーバレンダリング中には実行されません。
58+
* `useInsertionEffect` の内部から state を更新することはできません。
59+
* `useInsertionEffect` が実行される時点では、まだ ref はアタッチされていません。
60+
* `useInsertionEffect` DOM の更新前に実行される場合も後に実行される場合もあります。タイミングに関わらず、DOM が更新されていることを前提としてはいけません。
61+
* 他の種類のエフェクトはすべてのエフェクトにクリーンアップを実行してからすべてのエフェクトにセットアップを行います。一方で `useInsertionEffect` はコンポーネントごとにクリーンアップとセットアップをまとめて行います。結果的にクリーンアップとセットアップが「交互に実行される」ような挙動になります。
6262
---
6363
64-
## Usage {/*usage*/}
64+
## 使用法 {/*usage*/}
6565
66-
### Injecting dynamic styles from CSS-in-JS libraries {/*injecting-dynamic-styles-from-css-in-js-libraries*/}
66+
### CSS-in-JS ライブラリからの動的スタイル注入 {/*injecting-dynamic-styles-from-css-in-js-libraries*/}
6767
68-
Traditionally, you would style React components using plain CSS.
68+
従来、React コンポーネントのスタイル付けはプレーンな CSS を使用して行われていました。
6969
7070
```js
7171
// In your JS file:
@@ -75,20 +75,20 @@ Traditionally, you would style React components using plain CSS.
7575
.success { color: green; }
7676
```
7777
78-
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:
78+
チームによっては、CSS ファイルを書く代わりに JavaScript コード内で直接スタイルを記述することを好む場合があります。これには通常、CSS-in-JS ライブラリやツールが必要です。CSS-in-JS には以下の 3 つの一般的なアプローチがあります。
7979
80-
1. Static extraction to CSS files with a compiler
81-
2. Inline styles, e.g. `<div style={{ opacity: 1 }}>`
82-
3. Runtime injection of `<style>` tags
80+
1. コンパイラを使用した CSS ファイルへの静的な抽出
81+
2. インラインスタイル、例えば `<div style={{ opacity: 1 }}>`
82+
3. `<style>` タグのランタイム時の注入
8383
84-
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:**
84+
CSS-in-JS を使用する場合、最初の 2 つのアプローチの組み合わせ(静的スタイルには CSS ファイル、動的スタイルにはインラインスタイル)を推奨します。**ランタイム時の `<style>` タグの注入は、以下の 2 つの理由から推奨しません:**
8585
86-
1. Runtime injection forces the browser to recalculate the styles a lot more often.
87-
2. Runtime injection can be very slow if it happens at the wrong time in the React lifecycle.
86+
1. ランタイム時の注入は、ブラウザにスタイルの再計算を頻繁に強制します。
87+
2. ランタイム時の注入は、React ライフサイクル中の間違ったタイミングで行われると非常に遅くなることがあります。
8888
89-
The first problem is not solvable, but `useInsertionEffect` helps you solve the second problem.
89+
このうち最初の問題は解決不可能ですが、`useInsertionEffect` は 2 つ目の問題を解決するのに役立ちます。
9090
91-
Call `useInsertionEffect` to insert the styles before any layout effects fire:
91+
レイアウトエフェクトの発火前にスタイルを挿入するために `useInsertionEffect` を呼び出しましょう。
9292
9393
```js {4-11}
9494
// Inside your CSS-in-JS library
@@ -111,7 +111,7 @@ function Button() {
111111
}
112112
```
113113
114-
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:
114+
`useEffect` と同様に、`useInsertionEffect` はサーバ上では実行されません。サーバ上でどの CSS ルールが使用されたかを収集する必要がある場合、レンダー中に行うことができます。
115115
116116
```js {1,4-6}
117117
let collectedRulesSet = new Set();
@@ -127,14 +127,14 @@ function useCSS(rule) {
127127
}
128128
```
129129
130-
[Read more about upgrading CSS-in-JS libraries with runtime injection to `useInsertionEffect`.](https://github.com/reactwg/react-18/discussions/110)
130+
[`useInsertionEffect` でランタイム時にスタイルを注入するよう CSS-in-JS ライブラリをアップグレードする場合の詳細](https://github.com/reactwg/react-18/discussions/110)
131131
132132
<DeepDive>
133133
134-
#### How is this better than injecting styles during rendering or useLayoutEffect? {/*how-is-this-better-than-injecting-styles-during-rendering-or-uselayouteffect*/}
134+
#### この手法がレンダー中や useLayoutEffect でスタイルを注入するより優れている理由 {/*how-is-this-better-than-injecting-styles-during-rendering-or-uselayouteffect*/}
135135
136-
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.**
136+
レンダー中、React が[非ブロッキング更新](/reference/react/useTransition#marking-a-state-update-as-a-non-blocking-transition)を処理している最中にスタイルを挿入すると、ブラウザはコンポーネントツリーのレンダー中に毎フレームスタイルを再計算することになり、これは**非常に遅くなる**ことがあります。
137137
138-
`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.
138+
`useInsertionEffect` は、[`useLayoutEffect`](/reference/react/useLayoutEffect) [`useEffect`](/reference/react/useEffect) でスタイルを挿入するよりも優れています。なぜなら、他のエフェクトがあなたのコンポーネントで実行される時点で `<style>` タグがすでに挿入されていることが保証されるからです。さもないと、古くなったスタイルにより、通常のエフェクトでのレイアウト計算が正しくなくなってしまいます。
139139
140140
</DeepDive>

0 commit comments

Comments
 (0)