diff --git a/content/docs/hooks-state.md b/content/docs/hooks-state.md index 2a234af82..2d10d9f48 100644 --- a/content/docs/hooks-state.md +++ b/content/docs/hooks-state.md @@ -1,14 +1,14 @@ --- id: hooks-state -title: Using the State Hook +title: ステートフックの利用法 permalink: docs/hooks-state.html next: hooks-effect.html prev: hooks-overview.html --- -*Hooks* are a new addition in React 16.8. They let you use state and other React features without writing a class. +*フック (hook)* は React 16.8 で追加された新機能です。state などの React の機能を、クラスを書かずに使えるようになります。 -The [previous page](/docs/hooks-intro.html) introduced Hooks with this example: +[以前のページ](/docs/hooks-intro.html)で以下の例を挙げてフックの紹介を行いました: ```js{4-5} import React, { useState } from 'react'; @@ -28,11 +28,11 @@ function Example() { } ``` -We'll start learning about Hooks by comparing this code to an equivalent class example. +このコードをクラスによる等価版と比較しながらフックについて学び始めましょう。 -## Equivalent Class Example +## クラスによる同等の例 -If you used classes in React before, this code should look familiar: +以前 React でクラスを使っていたなら、このコードに馴染みがあるでしょう。 ```js class Example extends React.Component { @@ -56,15 +56,16 @@ class Example extends React.Component { } ``` -The state starts as `{ count: 0 }`, and we increment `state.count` when the user clicks a button by calling `this.setState()`. We'll use snippets from this class throughout the page. +state は `{ count: 0 }` から始まり、ユーザがボタンをクリックした時に `this.setState()` を呼ぶことで `state.count` をインクリメントします。このページの全体にわたってこのクラスからの例が出てきます。 ->Note +> 補足 > ->You might be wondering why we're using a counter here instead of a more realistic example. This is to help us focus on the API while we're still making our first steps with Hooks. +> もっと現実的な例ではなくカウンタを使っているのはなぜか気になるかもしれません。フックについての第一歩の説明ですので API にフォーカスするためです。 -## Hooks and Function Components +## フックと関数コンポーネント + +念のため、React の関数コンポーネントとはこのようなものです: -As a reminder, function components in React look like this: ```js const Example = (props) => { @@ -73,7 +74,7 @@ const Example = (props) => { } ``` -or this: +あるいはこのようなものです: ```js function Example(props) { @@ -82,13 +83,13 @@ function Example(props) { } ``` -You might have previously known these as "stateless components". We're now introducing the ability to use React state from these, so we prefer the name "function components". +これらのことを「ステートレスコンポーネント (stateless component)」だと理解していたかもしれません。これらの内部で React の state を利用できるようにしていますので、「関数コンポーネント (function component)」という名前を利用します。 -Hooks **don't** work inside classes. But you can use them instead of writing classes. +フックはクラスの内部では動作**しません**。クラスを書く代わりに使うものです。 -## What's a Hook? +## フックとは何か? -Our new example starts by importing the `useState` Hook from React: +この新しい例では、React から `useState` をインポートするところから始めましょう。 ```js{1} import React, { useState } from 'react'; @@ -98,17 +99,17 @@ function Example() { } ``` -**What is a Hook?** A Hook is a special function that lets you "hook into" React features. For example, `useState` is a Hook that lets you add React state to function components. We'll learn other Hooks later. +**Hook とは何?** フックとは React の機能に「接続 (hook into)」するための特別な関数です。例えば `useState` によって React の state の機能を関数コンポーネントに追加できます。他のフックについても後で学びます。 -**When would I use a Hook?** If you write a function component and realize you need to add some state to it, previously you had to convert it to a class. Now you can use a Hook inside the existing function component. We're going to do that right now! +**フックをいつ使うべき?** 関数コンポーネントを書いていて state が必要だと気付いた場合、これまではコンポーネントをクラスに変換する必要がありました。今後は、既に書いた関数コンポーネントの中でフックを使うことができます。早速やってみましょう! ->Note: +> 補足: > ->There are some special rules about where you can and can't use Hooks within a component. We'll learn them in [Rules of Hooks](/docs/hooks-rules.html). +> コンポーネント内のどこでフックが使えてどこで使えないかに関する特別なルールがあります。これについては[フックのルール](/docs/hooks-rules.html)で学びます。 -## Declaring a State Variable +## state 変数の宣言 -In a class, we initialize the `count` state to `0` by setting `this.state` to `{ count: 0 }` in the constructor: +クラスでは、コンストラクタ内で `this.state` を `{ count: 0 }` にセットするという方法で、state である `count` を `0` へと初期化します。 ```js{4-6} class Example extends React.Component { @@ -120,7 +121,7 @@ class Example extends React.Component { } ``` -In a function component, we have no `this`, so we can't assign or read `this.state`. Instead, we call the `useState` Hook directly inside our component: +関数コンポーネントには `this` は存在しませんので、`this.state` を読み書きすることはできません。その代わりに、コンポーネントの内部で直接 `useState` フックを使いましょう。 ```js{4,5} import React, { useState } from 'react'; @@ -130,13 +131,13 @@ function Example() { const [count, setCount] = useState(0); ``` -**What does calling `useState` do?** It declares a "state variable". Our variable is called `count` but we could call it anything else, like `banana`. This is a way to "preserve" some values between the function calls — `useState` is a new way to use the exact same capabilities that `this.state` provides in a class. Normally, variables "disappear" when the function exits but state variables are preserved by React. +**`useState` を呼ぶと何が起きるの?** これにより『state 変数』が宣言されます。我々の例では変数を `count` と名付けましたが、`banana` でも何でも好きな名前にすることができます。これが関数コールの間で値を『保持』しておくための方法です ― `useState` は、クラスにおいて `this.state` が提供するのと全く同じ機能を実現するための新しい方法です。通常、関数が終了すると変数は『消えて』しまいますが、state 変数は React によって保持されます。 -**What do we pass to `useState` as an argument?** The only argument to the `useState()` Hook is the initial state. Unlike with classes, the state doesn't have to be an object. We can keep a number or a string if that's all we need. In our example, we just want a number for how many times the user clicked, so pass `0` as initial state for our variable. (If we wanted to store two different values in state, we would call `useState()` twice.) +**引数として `useState` に何を渡すのか?** `useState()` フックの唯一の引数は state の初期値です。クラスの場合とは異なり、state はオブジェクトである必要はありません。数字や文字列が欲しいだけであればそれらを保持することができます。我々の例ではユーザがクリックした回数に対応する数字が欲しいだけですので、`0` を state 変数の初期値として渡します。(もし違う値を保持したい場合は `useState()` を 2 回呼ぶことになります) -**What does `useState` return?** It returns a pair of values: the current state and a function that updates it. This is why we write `const [count, setCount] = useState()`. This is similar to `this.state.count` and `this.setState` in a class, except you get them in a pair. If you're not familiar with the syntax we used, we'll come back to it [at the bottom of this page](/docs/hooks-state.html#tip-what-do-square-brackets-mean). +**`useState` は何を返すのか?** 現在の state と、それを更新するための関数とを、ペアにして返します。これが `const [count, setCount] = useState()` という書き方をした理由です。これはクラスにおける `this.state.count` と `this.setState` に似ていますが、それをペアとして手に入れる点が異なります。もしもここで使った構文になじみがない場合は、[このページの下部](/docs/hooks-state.html#tip-what-do-square-brackets-mean)で改めて説明します。 -Now that we know what the `useState` Hook does, our example should make more sense: +`useState` が何をやるのかが分かったので、最初の例の意味がより分かりやすくなりました: ```js{4,5} import React, { useState } from 'react'; @@ -146,32 +147,31 @@ function Example() { const [count, setCount] = useState(0); ``` -We declare a state variable called `count`, and set it to `0`. React will remember its current value between re-renders, and provide the most recent one to our function. If we want to update the current `count`, we can call `setCount`. +`count` という名前の state 変数を宣言しており、それを `0` にセットします。再レンダー間で React はその変数の現在値を記憶しており、最新の値を関数に渡します。現在の `count` の値を更新したい場合は、`setCount` を呼ぶことができます。 ->Note +> 補足 > ->You might be wondering: why is `useState` not named `createState` instead? +> どうして `createState` ではなく `useState` という名前なのか気になるでしょうか? > ->"Create" wouldn't be quite accurate because the state is only created the first time our component renders. During the next renders, `useState` gives us the current state. Otherwise it wouldn't be "state" at all! There's also a reason why Hook names *always* start with `use`. We'll learn why later in the [Rules of Hooks](/docs/hooks-rules.html). +> state が「作成」されるのはコンポーネントの初回レンダー時だけですので、`createState` という名前はあまり正確ではありません。次回以降のレンダー時には、`useState` からは既存の state の現在値を受け取ります。毎回作成していたのではそもそも「状態」になりませんね。また、フックの名前が*常に* `use` から始まることには理由があります。[フックのルール](/docs/hooks-rules.html)で改めて説明します。 -## Reading State +## state の読み出し -When we want to display the current count in a class, we read `this.state.count`: +クラス内で現在のカウント値を表示したい場合、`this.state.count` を読み出します: ```js
You clicked {this.state.count} times
``` -In a function, we can use `count` directly: - +関数内では、直接 `count` を使うことができます: ```jsYou clicked {count} times
``` -## Updating State +## state の更新 -In a class, we need to call `this.setState()` to update the `count` state: +クラスでは、`this.setState()` を呼ぶことで `count` ステートを更新します: ```js{1} ``` -In a function, we already have `setCount` and `count` as variables so we don't need `this`: +関数内では、すでに `setCount` と `count` を変数として受け取っていますので、`this` は必要ありません: ```js{1} ``` -## Recap +## まとめ -Let's now **recap what we learned line by line** and check our understanding. +では **これまで学んだことを 1 行ずつまとめて**、理解を確認しましょう。