Skip to content

Commit 52a7e96

Browse files
committed
Translate hooks-custom
1 parent 23e88ff commit 52a7e96

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

content/docs/hooks-custom.md

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
id: hooks-custom
3-
title: Building Your Own Hooks
3+
title: カスタムフックの作成
44
permalink: docs/hooks-custom.html
55
next: hooks-reference.html
66
prev: hooks-rules.html
77
---
88

9-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
9+
*フック*は近日実装予定の React の機能であり、state などの React の機能を、クラスを書かずに使えるようにするものです。現在 React v16.8.0-alpha.1 で利用可能です。
1010

11-
Building your own Hooks lets you extract component logic into reusable functions.
11+
自分独自のフックを作成することで、コンポーネントからロジックを抽出して再利用可能な関数を作ることが可能です。
1212

13-
When we were learning about [using the Effect Hook](/docs/hooks-effect.html#example-using-hooks-1), we saw this component from a chat application that displays a message indicating whether a friend is online or offline:
13+
以下のコンポーネントは[副作用フックの使い方](/docs/hooks-effect.html#example-using-hooks-1)について学んだ際に見たチャットアプリのコンポーネントであり、フレンドがオンラインかオフラインかを示すメッセージを表示します。
1414

1515
```js{4-15}
1616
import { useState, useEffect } from 'react';
@@ -36,7 +36,7 @@ function FriendStatus(props) {
3636
}
3737
```
3838

39-
Now let's say that our chat application also has a contact list, and we want to render names of online users with a green color. We could copy and paste similar logic above into our `FriendListItem` component but it wouldn't be ideal:
39+
さて、このチャットアプリには連絡先リストもあって、そこではオンラインのユーザを緑色で表示したいとしましょう。新しい `FriendListItem` コンポーネントに似たようなロジックをコピーペーストしても構いませんが、それは理想的ではないでしょう:
4040

4141
```js{4-15}
4242
import { useState, useEffect } from 'react';
@@ -63,15 +63,15 @@ function FriendListItem(props) {
6363
}
6464
```
6565

66-
Instead, we'd like to share this logic between `FriendStatus` and `FriendListItem`.
66+
代わりに、このロジックを `FriendStatus` `FriendListeItem` 間で共有したいと思います。
6767

68-
Traditionally in React, we've had two popular ways to share stateful logic between components: [render props](/docs/render-props.html) and [higher-order components](/docs/higher-order-components.html). We will now look at how Hooks solve many of the same problems without forcing you to add more components to the tree.
68+
これまで React では、ステートを有するロジックをコンポーネント間で共有するための人気の手法が 2 つありました。[レンダープロップ](/docs/render-props.html) [高階コンポーネント](/docs/higher-order-components.html)です。ツリーに新しいコンポーネントを加える必要なしに、フックが同じ問題をどのように解決するのかを見ていきましょう。
6969

70-
## Extracting a Custom Hook
70+
## カスタムフックの抽出
7171

72-
When we want to share logic between two JavaScript functions, we extract it to a third function. Both components and Hooks are functions, so this works for them too!
72+
2 つの JavaScript の関数間でロジックを共有したい場合、それを別の関数に抽出します。コンポーネントもフックも関数ですので、同じ方法が使えます!
7373

74-
**A custom Hook is a JavaScript function whose name starts with "`use`" and that may call other Hooks.** For example, `useFriendStatus` below is our first custom Hook:
74+
**カスタムフックとは、名前が "`use`" で始まり、ほかのフックを呼び出せる JavaScript の関数のことです。** 例えば、以下の `useFriendStatus` が我々の最初のカスタムフックの例です:
7575

7676
```js{3}
7777
import { useState, useEffect } from 'react';
@@ -94,11 +94,11 @@ function useFriendStatus(friendID) {
9494
}
9595
```
9696

97-
There's nothing new inside of it -- the logic is copied from the components above. Just like in a component, make sure to only call other Hooks unconditionally at the top level of your custom Hook.
97+
新しいことは何もありません。ロジックは上記のコンポーネントからコピーしてきただけです。コンポーネントの時と同様に、他のフックを呼ぶときはカスタムフックのトップレベルで無条件に呼び出していることを確認してください。
9898

99-
Unlike a React component, a custom Hook doesn't need to have a specific signature. We can decide what it takes as arguments, and what, if anything, it should return. In other words, it's just like a normal function. Its name should always start with `use` so that you can tell at a glance that the [rules of Hooks](/docs/hooks-rules.html) apply to it.
99+
React のコンポーネントと違い、カスタムフックは特定のシグネチャを持つ必要はありません。何を引数として受け取り、そして(必要なら)何を返すのか、といったことは自分で決めることができます。別の言い方をすると、普通の関数と同じだということです。一目で[フックのルール](/docs/hooks-rules.html)が適用されるものだと分かるようにするために、名前は `use` で始めるべきです。
100100

101-
The purpose of our `useFriendStatus` Hook is to subscribe us to a friend's status. This is why it takes `friendID` as an argument, and returns whether this friend is online:
101+
この `useFriendStatus` の目的はフレンドのステータスを購読するというものです。ですので `friendID` を引数として持ち、そのフレンドがオンラインかどうかを返します。
102102

103103
```js
104104
function useFriendStatus(friendID) {
@@ -110,13 +110,13 @@ function useFriendStatus(friendID) {
110110
}
111111
```
112112

113-
Now let's see how we can use our custom Hook.
113+
ではこのカスタムフックの使い方を見ていきましょう。
114114

115-
## Using a Custom Hook
115+
## カスタムフックを使う
116116

117-
In the beginning, our stated goal was to remove the duplicated logic from the `FriendStatus` and `FriendListItem` components. Both of them want to know whether a friend is online.
117+
そもそもの我々の目的は `FriendStatus` `FriendListItem` コンポーネントでの重複したロジックを取り除くことでした。どちらのコンポーネントもフレンドがオンラインかどうかを知りたいのです。
118118

119-
Now that we've extracted this logic to a `useFriendStatus` hook, we can *just use it:*
119+
既にロジックを `useFriendStatus` フックへと抽出したので、それを*ただ単に使えばいい*のです:
120120

121121
```js{2}
122122
function FriendStatus(props) {
@@ -141,19 +141,19 @@ function FriendListItem(props) {
141141
}
142142
```
143143

144-
**Is this code equivalent to the original examples?** Yes, it works in exactly the same way. If you look closely, you'll notice we didn't make any changes to the behavior. All we did was to extract some common code between two functions into a separate function. **Custom Hooks are a convention that naturally follows from the design of Hooks, rather than a React feature.**
144+
**このコードは元のコードと同等?** はい、全く同じように動作します。注意深く見れば、ふるまいに何の変更も加えていないということが分かります。やったのは、共通のコードを別の関数に抽出したということだけです。**カスタムフックは React の機能というよりは、フックの設計から自然と導かれる慣習のようなものです。**
145145

146-
**Do I have to name my custom Hooks starting with “`use`”?** Please do. This convention is very important. Without it, we wouldn't be able to automatically check for violations of [rules of Hooks](/docs/hooks-rules.html) because we couldn't tell if a certain function contains calls to Hooks inside of it.
146+
**カスタムフックは "`use`" という名前で始めるべき?** ぜひそうしてください。この規約はとても重要です。この規約がなければ、ある関数が内部でフックを呼んでいるかどうかを知る方法がなくなり、[フックのルール](/docs/hooks-rules.html)の違反を自動でチェックすることができなくなります。
147147

148-
**Do two components using the same Hook share state?** No. Custom Hooks are a mechanism to reuse *stateful logic* (such as setting up a subscription and remembering the current value), but every time you use a custom Hook, all state and effects inside of it are fully isolated.
148+
**同じフックを使うコンポーネントは state を共有する?** いいえ。カスタムフックは *state を使うロジック*(データの購読を登録したり現在の値を覚えておいたり)を共有するためのものですが、カスタムフックを使う場所ごとで、内部の state や副作用は完全に分離しています。
149149

150-
**How does a custom Hook get isolated state?** Each *call* to a Hook gets isolated state. Because we call `useFriendStatus` directly, from React's point of view our component just calls `useState` and `useEffect`. And as we [learned](/docs/hooks-state.html#tip-using-multiple-state-variables) [earlier](/docs/hooks-effect.html#tip-use-multiple-effects-to-separate-concerns), we can call `useState` and `useEffect` many times in one component, and they will be completely independent.
150+
**どのようにしてカスタムフックは独立したステートを得るのか?** それぞれのフックの*呼び出し*が独立した state を得ます。`useFriendStatus` を直接呼びだしていますので、React から見れば我々のコンポーネントが `useState` `useEffect` を呼んだ場合と変わりません。すでに[ここ](/docs/hooks-state.html#tip-using-multiple-state-variables)[ここ](/docs/hooks-effect.html#tip-use-multiple-effects-to-separate-concerns)で学んだ通り、`useState` `useEffect` はひとつのコンポーネント内で複数回呼ぶことができ、それらは完全に独立しています。
151151

152-
### Tip: Pass Information Between Hooks
152+
### ヒント:フック間で情報を受け渡す
153153

154-
Since Hooks are functions, we can pass information between them.
154+
フックは関数ですので、フック間で情報を受け渡すことができます。
155155

156-
To illustrate this, we'll use another component from our hypothetical chat example. This is a chat message recipient picker that displays whether the currently selected friend is online:
156+
これを例示するため、我々のチャットの例で、別のコンポーネントを使うことにしましょう。これはチャットの受信者を選ぶ画面であり、現在選択中のフレンドがオンラインかどうかを表示します。
157157

158158
```js{8-9,13}
159159
const friendList = [
@@ -184,24 +184,24 @@ function ChatRecipientPicker() {
184184
}
185185
```
186186

187-
We keep the currently chosen friend ID in the `recipientID` state variable, and update it if the user chooses a different friend in the `<select>` picker.
187+
現在選択中のフレンド ID `recipientID` という state 変数に保持し、`<select>` ピッカー内で別のフレンドが選択されるごとにそれを更新します。
188188

189-
Because the `useState` Hook call gives us the latest value of the `recipientID` state variable, we can pass it to our custom `useFriendStatus` Hook as an argument:
189+
`useState` フックは `recipientID` という state 変数の最新の値を返しますので、それを `useFriendStatus` カスタムフックに引数として渡すことができます。
190190

191191
```js
192192
const [recipientID, setRecipientID] = useState(1);
193193
const isRecipientOnline = useFriendStatus(recipientID);
194194
```
195195

196-
This lets us know whether the *currently selected* friend is online. If we pick a different friend and update the `recipientID` state variable, our `useFriendStatus` Hook will unsubscribe from the previously selected friend, and subscribe to the status of the newly selected one.
196+
これにより*現在選択中の*フレンドがオンラインかどうかが分かります。別のフレンドを選択して `recipientID` 変数が更新された場合、`useFriendStatus` フックはこれまで選択されていたフレンドを購読解除して、新しく選択されたフレンドのステータスを購読開始します。
197197

198198
## `useYourImagination()`
199199

200-
Custom Hooks offer the flexibility of sharing logic that wasn't possible in React components before. You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven't considered. What's more, you can build Hooks that are just as easy to use as React's built-in features.
200+
カスタムフックにより、これまでの React コンポーネントでは不可能であった、ロジック共有に関する柔軟性が得られます。フォーム操作、アニメーション、宣言的データ購読、タイマー、さらには我々が思いついたことのない多様なユースケースに対するカスタムフックを記述することが可能です。何より、作ったカスタムフックは React の組み込み機能と同じくらい簡単に使えるようになるのです。
201201

202-
Try to resist adding abstraction too early. Now that function components can do more, it's likely that the average function component in your codebase will become longer. This is normal -- don't feel like you *have to* immediately split it into Hooks. But we also encourage you to start spotting cases where a custom Hook could hide complex logic behind a simple interface, or help untangle a messy component.
202+
あまり焦って抽象化を加えないようにしましょう。関数コンポーネントがやれることが増えたので、平均的な関数コンポーネントはこれまでより長いものになるでしょう。それは普通のことですので、いますぐカスタムフックに分割しないと*いけない*とは考えないでください。一方で、カスタムフックをどこで使えば複雑なロジックをシンプルなインターフェースに置き換えたり、ごちゃっとしたコンポーネントを整理したりできるのか、考え始めることをお勧めします。
203203

204-
For example, maybe you have a complex component that contains a lot of local state that is managed in an ad-hoc way. `useState` doesn't make centralizing the update logic any easier so might you prefer to write it as a [Redux](https://redux.js.org/) reducer:
204+
一例として、その場しのぎで多くのローカル state が含まれるようになった複雑なコンポーネントをお持ちかもしれません。`useState` を使っても更新ロジックの集中化が簡単になるわけではありませんので、それを [Redux](https://redux.js.org/) のリデューサ (reducer) で書くたくなることがあるでしょう:
205205

206206
```js
207207
function todosReducer(state, action) {
@@ -218,9 +218,9 @@ function todosReducer(state, action) {
218218
}
219219
```
220220

221-
Reducers are very convenient to test in isolation, and scale to express complex update logic. You can further break them apart into smaller reducers if necessary. However, you might also enjoy the benefits of using React local state, or might not want to install another library.
221+
リデューサは単独でのテストが非常にやりやすく、複雑な更新ロジックを表現する場合でもスケールします。必要に応じて後でより小さなリデューサに分割することも可能です。しかし、React のローカル state による手軽さの方が好ましい場合もあるでしょうし、他のライブラリをインストールしたくない場合もあるでしょう。
222222

223-
So what if we could write a `useReducer` Hook that lets us manage the *local* state of our component with a reducer? A simplified version of it might look like this:
223+
そこで、`useReducer` というフックを書いて、コンポーネントの*ローカル* state をリデューサで管理できるとしたらどうでしょうか? 簡略版では以下のようなものになるでしょう:
224224

225225
```js
226226
function useReducer(reducer, initialState) {
@@ -235,7 +235,7 @@ function useReducer(reducer, initialState) {
235235
}
236236
```
237237

238-
Now we could use it in our component, and let the reducer drive its state management:
238+
これをコンポーネント内で使うことができ、リデューサを活用してステート管理ができるようになります:
239239

240240
```js{2}
241241
function Todos() {
@@ -249,4 +249,4 @@ function Todos() {
249249
}
250250
```
251251

252-
The need to manage local state with a reducer in a complex component is common enough that we've built the `useReducer` Hook right into React. You'll find it together with other built-in Hooks in the [Hooks API reference](/docs/hooks-reference.html).
252+
ローカルステートをリデューサで管理したいという要求はとてもよくあるので、React にその機能を含めてあります。[Hooks API reference](/docs/hooks-reference.html) のページで他のビルトインフックと共に解説しています。

0 commit comments

Comments
 (0)