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 38d87dc
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: src/content/reference/eslint-plugin-react-hooks/lints/component-hook-factories.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,17 @@ title: component-hook-factories
4
4
5
5
<Intro>
6
6
7
-
Validates against higher order functions defining nested components or hooks. Components and hooks should be defined at the module level.
7
+
コンポーネントやフックをネストして定義する高階関数 (higher order function) を使っていないか検証します。コンポーネントやフックは、モジュールレベルで定義するべきです。
8
8
9
9
</Intro>
10
10
11
-
## Rule Details {/*rule-details*/}
11
+
## ルールの詳細 {/*rule-details*/}
12
12
13
-
Defining components or hooks inside other functions creates new instances on every call. React treats each as a completely different component, destroying and recreating the entire component tree, losing all state, and causing performance problems.
13
+
関数内でコンポーネントやフックを定義すると、呼び出しのたびに新しい関数インスタンスが作成されます。React はそれぞれをまったく別のコンポーネントとして扱うため、コンポーネントツリー全体が破棄されて再作成され、すべての state が失われ、パフォーマンスの問題が発生します。
14
14
15
-
### Invalid {/*invalid*/}
15
+
### 無効な例 {/*invalid*/}
16
16
17
-
Examples of incorrect code for this rule:
17
+
このルールに違反するコードの例です。
18
18
19
19
```js {expectedErrors: {'react-compiler': [14]}}
20
20
// ❌ Factory function creating components
@@ -41,9 +41,9 @@ function createCustomHook(endpoint) {
41
41
}
42
42
```
43
43
44
-
### Valid {/*valid*/}
44
+
### 有効な例 {/*valid*/}
45
45
46
-
Examples of correct code for this rule:
46
+
このルールに従ったコードの例です。
47
47
48
48
```js
49
49
// ✅ Component defined at module level
@@ -57,11 +57,11 @@ function useData(endpoint) {
57
57
}
58
58
```
59
59
60
-
## Troubleshooting {/*troubleshooting*/}
60
+
## トラブルシューティング {/*troubleshooting*/}
61
61
62
-
### I need dynamic component behavior {/*dynamic-behavior*/}
62
+
### コンポーネントの動作を動的に変えたい {/*dynamic-behavior*/}
63
63
64
-
You might think you need a factory to create customized components:
Copy file name to clipboardExpand all lines: src/content/reference/eslint-plugin-react-hooks/lints/static-components.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,17 @@ title: static-components
4
4
5
5
<Intro>
6
6
7
-
Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering.
Components defined inside other components are recreated on every render. React sees each as a brand new component type, unmounting the old one and mounting the new one, destroying all state and DOM nodes in the process.
13
+
他のコンポーネント内で定義したコンポーネントは、レンダーのたびに再作成されます。React はそれぞれをまったく新しいコンポーネント型として扱うため、古いものをアンマウントし、新しいものをマウントします。その過程で、state と DOM ノードがすべて破棄されてしまいます。
14
14
15
-
### Invalid {/*invalid*/}
15
+
### 無効な例 {/*invalid*/}
16
16
17
-
Examples of incorrect code for this rule:
17
+
このルールに違反するコードの例です。
18
18
19
19
```js
20
20
// ❌ Component defined inside component
@@ -37,9 +37,9 @@ function Parent({type}) {
37
37
}
38
38
```
39
39
40
-
### Valid {/*valid*/}
40
+
### 有効な例 {/*valid*/}
41
41
42
-
Examples of correct code for this rule:
42
+
このルールに従ったコードの例です。
43
43
44
44
```js
45
45
// ✅ Components at module level
@@ -55,11 +55,11 @@ function Parent({type}) {
55
55
}
56
56
```
57
57
58
-
## Troubleshooting {/*troubleshooting*/}
58
+
## トラブルシューティング {/*troubleshooting*/}
59
59
60
-
### I need to render different components conditionally {/*conditional-components*/}
You might define components inside to access local state:
62
+
以下のように、ローカルの state にアクセスする目的で、コンポーネントの内側でコンポーネントを定義したいのかもしれません。
63
63
64
64
```js {expectedErrors: {'react-compiler': [13]}}
65
65
// ❌ Wrong: Inner component to access parent state
@@ -78,7 +78,7 @@ function Parent() {
78
78
}
79
79
```
80
80
81
-
Pass data as props instead:
81
+
代わりに、データを props として渡してください。
82
82
83
83
```js
84
84
// ✅ Better: Pass props to static component
@@ -98,6 +98,6 @@ function Parent() {
98
98
99
99
<Note>
100
100
101
-
If you find yourself wanting to define components inside other components to access local variables, that's a sign you should be passing props instead. This makes components more reusable and testable.
0 commit comments