You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f2c1ae3
Browse filesBrowse the repository at this point in the historyBrowse files
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 が変わると自然に再実行され、余分なレンダーサイクルを引き起こしません。
14
14
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.
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.
This rule catches several patterns where synchronous setState is used unnecessarily:
21
+
このルールは、同期的な setState が不要なのに使われているパターンを検出します。
22
22
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
+
-レンダー中ではなくエフェクト内でデータを変換している
26
26
27
-
### Invalid {/*invalid*/}
27
+
### 無効な例 {/*invalid*/}
28
28
29
-
Examples of incorrect code for this rule:
29
+
このルールに違反するコードの例です。
30
30
31
31
```js
32
32
// ❌ Synchronous setState in effect
@@ -67,9 +67,9 @@ function Component({selectedId, items}) {
67
67
}
68
68
```
69
69
70
-
### Valid {/*valid*/}
70
+
### 有効な例 {/*valid*/}
71
71
72
-
Examples of correct code for this rule:
72
+
このルールに従ったコードの例です。
73
73
74
74
```js
75
75
// ✅ setState in an effect is fine if the value comes from a ref
@@ -90,4 +90,4 @@ function Component({selectedId, items}) {
90
90
}
91
91
```
92
92
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