Skip to content

Commit f2c1ae3

Browse files
committed
Translate set-state-in-effect lint rule
1 parent 37a7f80 commit f2c1ae3

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

‎src/content/reference/eslint-plugin-react-hooks/lints/set-state-in-effect.md‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ title: set-state-in-effect
44

55
<Intro>
66

7-
Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance.
7+
パフォーマンスを低下させる再レンダーにつながる、エフェクト内での同期的な setState 呼び出しを行っていないか検証します。
88

99
</Intro>
1010

11-
## Rule Details {/*rule-details*/}
11+
## ルールの詳細 {/*rule-details*/}
1212

13-
Setting state immediately inside an effect forces React to restart the entire render cycle. When you update state in an effect, React must re-render your component, apply changes to the DOM, and then run effects again. This creates an extra render pass that could have been avoided by transforming data directly during render or deriving state from props. Transform data at the top level of your component instead. This code will naturally re-run when props or state change without triggering additional render cycles.
13+
エフェクト内で即座に state を設定すると、React はレンダーサイクル全体を最初からやり直すことになります。エフェクト内で state を更新すると、React はコンポーネントを再レンダーし、DOM に変更を適用してから、エフェクトを再び実行しなければなりません。これにより、レンダー中に直接データを変換したり、props から state を導出したりすれば避けられたはずの、余分なレンダーが発生します。代わりに、コンポーネントのトップレベルでデータを変換してください。このコードは、props や state が変わると自然に再実行され、余分なレンダーサイクルを引き起こしません。
1414

15-
Synchronous `setState` calls in effects trigger immediate re-renders before the browser can paint, causing performance issues and visual jank. React has to render twice: once to apply the state update, then again after effects run. This double rendering is wasteful when the same result could be achieved with a single render.
15+
エフェクト内で同期的に `setState` を呼び出すと、ブラウザがペイントする前に即座に再レンダーが起き、パフォーマンスの問題や表示のちらつきを引き起こします。React は、state の更新を適用するために 1 回、そしてエフェクトの実行後にもう 1 回、合計 2 回レンダーすることになります。1 回のレンダーで同じ結果を得られる場合、この二重のレンダーは無駄です。
1616

17-
In many cases, you may also not need an effect at all. Please see [You Might Not Need an Effect](/learn/you-might-not-need-an-effect) for more information.
17+
多くの場合、そもそもエフェクト自体が不要かもしれません。詳細は、[そのエフェクトは不要かも](/learn/you-might-not-need-an-effect)を参照してください。
1818

19-
## Common Violations {/*common-violations*/}
19+
## よくある違反 {/*common-violations*/}
2020

21-
This rule catches several patterns where synchronous setState is used unnecessarily:
21+
このルールは、同期的な setState が不要なのに使われているパターンを検出します。
2222

23-
- Setting loading state synchronously
24-
- Deriving state from props in effects
25-
- Transforming data in effects instead of render
23+
- ローディングを表す state を同期的に設定している
24+
- エフェクト内で props から state を導出している
25+
- レンダー中ではなくエフェクト内でデータを変換している
2626

27-
### Invalid {/*invalid*/}
27+
### 無効な例 {/*invalid*/}
2828

29-
Examples of incorrect code for this rule:
29+
このルールに違反するコードの例です。
3030

3131
```js
3232
// ❌ Synchronous setState in effect
@@ -67,9 +67,9 @@ function Component({selectedId, items}) {
6767
}
6868
```
6969

70-
### Valid {/*valid*/}
70+
### 有効な例 {/*valid*/}
7171

72-
Examples of correct code for this rule:
72+
このルールに従ったコードの例です。
7373

7474
```js
7575
// ✅ setState in an effect is fine if the value comes from a ref
@@ -90,4 +90,4 @@ function Component({selectedId, items}) {
9090
}
9191
```
9292
93-
**When something can be calculated from the existing props or state, don't put it in state.** Instead, calculate it during rendering. This makes your code faster, simpler, and less error-prone. Learn more in [You Might Not Need an Effect](/learn/you-might-not-need-an-effect).
93+
**既存の props や state から計算できるものを、state に保存しないでください**。代わりに、レンダー中に計算してください。これにより、コードが高速でシンプルになり、間違いが起きにくくなります。詳細は、[そのエフェクトは不要かも](/learn/you-might-not-need-an-effect)を参照してください。

0 commit comments

Comments
 (0)