Skip to content

Commit 6a8cb3a

Browse files
authored
Translate hooks-rules (#64)
* Translate hooks-rules * Fix: 2つの * Fix textlint errors
1 parent 9438c07 commit 6a8cb3a

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

content/docs/hooks-rules.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
---
22
id: hooks-rules
3-
title: Rules of Hooks
3+
title: フックのルール
44
permalink: docs/hooks-rules.html
55
next: hooks-custom.html
66
prev: hooks-effect.html
77
---
88

9-
*Hooks* are a new addition in React 16.8. They let you use state and other React features without writing a class.
9+
*フック (hook)*React 16.8 で追加された新機能です。state などの React の機能を、クラスを書かずに使えるようになります。
1010

11-
Hooks are JavaScript functions, but you need to follow two rules when using them. We provide a [linter plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to enforce these rules automatically:
11+
フックは JavaScript の関数ですが、それらを使う際には以下の 2 つのルールに従う必要があります。我々は自動的にこのルールを強制するための [linter プラグイン](https://www.npmjs.com/package/eslint-plugin-react-hooks) を提供しています。
1212

13-
### Only Call Hooks at the Top Level {#only-call-hooks-at-the-top-level}
13+
### フックを呼び出すのはトップレベルのみ {#only-call-hooks-at-the-top-level}
1414

15-
**Don't call Hooks inside loops, conditions, or nested functions.** Instead, always use Hooks at the top level of your React function. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That's what allows React to correctly preserve the state of Hooks between multiple `useState` and `useEffect` calls. (If you're curious, we'll explain this in depth [below](#explanation).)
15+
**フックをループや条件分岐、あるいはネストされた関数内で呼び出してはいけません。**代わりに、あなたの React の関数のトップレベルでのみ呼び出してください。これを守ることで、コンポーネントがレンダーされる際に毎回同じ順番で呼び出されるということが保証されます。これが、複数回 `useState` `useEffect` が呼び出された場合でも Raect がフックの状態を正しく保持するための仕組みです。(興味がある場合は[ページ下部](#explanation)で詳しく説明しています)
1616

17-
### Only Call Hooks from React Functions {#only-call-hooks-from-react-functions}
17+
### フックを呼び出すのは React の関数内のみ {#only-call-hooks-from-react-functions}
1818

19-
**Don't call Hooks from regular JavaScript functions.** Instead, you can:
19+
**フックを通常の JavaScript 関数から呼び出さないでください。**代わりに以下のようにします。
2020

21-
*Call Hooks from React function components.
22-
*Call Hooks from custom Hooks (we'll learn about them [on the next page](/docs/hooks-custom.html)).
21+
- ✅ React の関数コンポーネント内から呼び出す。
22+
-カスタムフック内([次のページで説明します](/docs/hooks-custom.html))から呼び出す。
2323

24-
By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.
24+
このルールを守ることで、コンポーネント内のすべての state を使うロジックがソースコードから間違いなく参照可能になります。
2525

26-
## ESLint Plugin {#eslint-plugin}
26+
## ESLint プラグイン {#eslint-plugin}
2727

28-
We released an ESLint plugin called [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) that enforces these two rules. You can add this plugin to your project if you'd like to try it:
28+
これらの 2 つのルールを強制できる [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) という ESLint のプラグインをリリースしました。試したい場合はあなたのプロジェクトに以下のようにして加えることができます。
2929

3030
```bash
3131
npm install eslint-plugin-react-hooks
@@ -45,13 +45,13 @@ npm install eslint-plugin-react-hooks
4545
}
4646
```
4747

48-
In the future, we intend to include this plugin by default into Create React App and similar toolkits.
48+
将来的にはこのプラグインを Create React App や類似のツールキットにデフォルトで含めるつもりです。
4949

50-
**You can skip to the next page explaining how to write [your own Hooks](/docs/hooks-custom.html) now.** On this page, we'll continue by explaining the reasoning behind these rules.
50+
**次のページまで飛ばして[独自のフック](/docs/hooks-custom.html)を書く方法について学んでも構いません。**このページの続きの部分ではこれらのルールの背後にある根拠について述べていきます。
5151

52-
## Explanation {#explanation}
52+
## 解説 {#explanation}
5353

54-
As we [learned earlier](/docs/hooks-state.html#tip-using-multiple-state-variables), we can use multiple State or Effect Hooks in a single component:
54+
[既に学んだ通り](/docs/hooks-state.html#tip-using-multiple-state-variables)、ひとつのコンポーネント内で複数の state や副作用を使うことができます。
5555

5656
```js
5757
function Form() {
@@ -75,7 +75,7 @@ function Form() {
7575
}
7676
```
7777

78-
So how does React know which state corresponds to which `useState` call? The answer is that **React relies on the order in which Hooks are called**. Our example works because the order of the Hook calls is the same on every render:
78+
では React は、どの `useState` の呼び出しがどの state に対応するのか、どうやって知るのでしょうか? その答えは「**React はフックが呼ばれる順番に依存している**」です。我々の例が動作するのは、フックの呼び出しの順序が毎回のレンダーごとに同じだからです。
7979

8080
```js
8181
// ------------
@@ -97,7 +97,7 @@ useEffect(updateTitle) // 4. Replace the effect for updating the title
9797
// ...
9898
```
9999

100-
As long as the order of the Hook calls is the same between renders, React can associate some local state with each of them. But what happens if we put a Hook call (for example, the `persistForm` effect) inside a condition?
100+
フックへの呼び出しの順番がレンダー間で変わらなければ、React はそれらのフックにローカル state を割り当てることができます。ですがフックの呼び出しを条件分岐内(例えば `persistForm` 副作用の内部で)で行ったらどうなるでしょうか?
101101

102102
```js
103103
// 🔴 We're breaking the first rule by using a Hook in a condition
@@ -108,7 +108,7 @@ As long as the order of the Hook calls is the same between renders, React can as
108108
}
109109
```
110110

111-
The `name !== ''` condition is `true` on the first render, so we run this Hook. However, on the next render the user might clear the form, making the condition `false`. Now that we skip this Hook during rendering, the order of the Hook calls becomes different:
111+
`name !== ''` という条件は初回のレンダー時には `true` なので、フックは実行されます。しかし次回のレンダー時にはユーザがフォームをクリアしているかもしれず、その場合にこの条件は `false` になります。するとレンダー途中でこのフックがスキップされるため、フックの呼ばれる順番が変わってしまいます。
112112

113113
```js
114114
useState('Mary') // 1. Read the name state variable (argument is ignored)
@@ -117,9 +117,9 @@ useState('Poppins') // 🔴 2 (but was 3). Fail to read the surname state
117117
useEffect(updateTitle) // 🔴 3 (but was 4). Fail to replace the effect
118118
```
119119

120-
React wouldn't know what to return for the second `useState` Hook call. React expected that the second Hook call in this component corresponds to the `persistForm` effect, just like during the previous render, but it doesn't anymore. From that point, every next Hook call after the one we skipped would also shift by one, leading to bugs.
120+
React は 2 つ目の `useState` の呼び出しに対して何を返せばいいのか分からなくなります。React は 2 つめのフックの呼び出しは前回レンダー時と同様に `persistForm` に対応するものだと期待しているのですが、それが成り立たなくなっています。この部分より先では、スキップされたもの以降のすべてのフックがひとつずつずれているため、バグを引き起こします。
121121

122-
**This is why Hooks must be called on the top level of our components.** If we want to run an effect conditionally, we can put that condition *inside* our Hook:
122+
**これがフックを呼び出すのがトップレベルのみでなければならない理由です。**条件付きで副作用を走らせたい場合は、その条件をフックの*内部*に入れることができます:
123123

124124
```js
125125
useEffect(function persistForm() {
@@ -130,8 +130,8 @@ React wouldn't know what to return for the second `useState` Hook call. React ex
130130
});
131131
```
132132

133-
**Note that you don't need to worry about this problem if you use the [provided lint rule](https://www.npmjs.com/package/eslint-plugin-react-hooks).** But now you also know *why* Hooks work this way, and which issues the rule is preventing.
133+
**[上記の lint ルール](https://www.npmjs.com/package/eslint-plugin-react-hooks)を使えばこの問題について心配する必要はない、ということに注意してください。**しかしフックが*なぜ*このように動作するのか、このルールがどんな問題を防いでいるのかについて学びました。
134134

135-
## Next Steps {#next-steps}
135+
## 次のステップ {#next-steps}
136136

137-
Finally, we're ready to learn about [writing your own Hooks](/docs/hooks-custom.html)! Custom Hooks let you combine Hooks provided by React into your own abstractions, and reuse common stateful logic between different components.
137+
ついに[自分独自のフックの書き方](/docs/hooks-custom.html)について学ぶ準備ができました! カスタムフックを使えば React から提供されるフックを組み合わせて自分独自の抽象化を作り出し、複数の異なるコンポーネント間で state を使う共通のロジックを再利用することができるようになります。

0 commit comments

Comments
 (0)