diff --git a/src/content/learn/queueing-a-series-of-state-updates.md b/src/content/learn/queueing-a-series-of-state-updates.md
index a63b7205b..3025407a3 100644
--- a/src/content/learn/queueing-a-series-of-state-updates.md
+++ b/src/content/learn/queueing-a-series-of-state-updates.md
@@ -1,23 +1,23 @@
---
-title: Queueing a Series of State Updates
+title: 一連の state の更新をキューに入れる
---
-Setting a state variable will queue another render. But sometimes you might want to perform multiple operations on the value before queueing the next render. To do this, it helps to understand how React batches state updates.
+state 変数をセットすることで、新しいレンダーがキューに予約されます。しかし、次のレンダーをキューに入れる前に、state の値に対して複数の操作を行いたい場合があります。このためには、React が state の更新をどのようにバッチ処理(batching, 一括処理)するのかについて理解することが役立ちます。
-* What "batching" is and how React uses it to process multiple state updates
-* How to apply several updates to the same state variable in a row
+* 「バッチ処理」とは何か、React が複数の state 更新を処理する際にどのように使用されるのか
+* 同じ state 変数に対し連続して複数の更新を適用する方法
-## React batches state updates {/*react-batches-state-updates*/}
+## React は state 更新をまとめて処理する {/*react-batches-state-updates*/}
-You might expect that clicking the "+3" button will increment the counter three times because it calls `setNumber(number + 1)` three times:
+以下で "+3" ボタンをクリックした場合、`setNumber(number + 1)` を 3 回呼び出しているので、カウンタが 3 回インクリメントされると思うかもしれません。
@@ -47,7 +47,7 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
-However, as you might recall from the previous section, [each render's state values are fixed](/learn/state-as-a-snapshot#rendering-takes-a-snapshot-in-time), so the value of `number` inside the first render's event handler is always `0`, no matter how many times you call `setNumber(1)`:
+しかし、前のセクションで説明したように、[個々のレンダー内の state 値は固定です](/learn/state-as-a-snapshot#rendering-takes-a-snapshot-in-time)。従って `setNumber(1)` を何度呼び出しても、最初のレンダー内ではイベントハンドラ内の `number` の値は常に `0` です。
```js
setNumber(0 + 1);
@@ -55,21 +55,21 @@ setNumber(0 + 1);
setNumber(0 + 1);
```
-But there is one other factor at play here. **React waits until *all* code in the event handlers has run before processing your state updates.** This is why the re-render only happens *after* all these `setNumber()` calls.
+しかしながら、ここにもう 1 つ別の要素が関わってきます。**イベントハンドラ内のすべてのコードが実行されるまで、React は state の更新処理を待機します**。このため、再レンダーはこれらの `setNumber()` 呼び出しがすべて終わった後で行われます。
-This might remind you of a waiter taking an order at the restaurant. A waiter doesn't run to the kitchen at the mention of your first dish! Instead, they let you finish your order, let you make changes to it, and even take orders from other people at the table.
+レストランで注文を取るウェイターの話を思い出すかもしれません。ウェイターは最初の料理の注文を聞いた瞬間にキッチンにかけこむわけではありません! 代わりに、客の注文を最後まで聞き、訂正がある場合はそれも聞き取り、さらにはテーブルの他の客からの注文もまとめて受け取るはずです。
-
+
-This lets you update multiple state variables--even from multiple components--without triggering too many [re-renders.](/learn/render-and-commit#re-renders-when-state-updates) But this also means that the UI won't be updated until _after_ your event handler, and any code in it, completes. This behavior, also known as **batching,** makes your React app run much faster. It also avoids dealing with confusing "half-finished" renders where only some of the variables have been updated.
+これにより、複数の state 変数(複数のコンポーネントからの場合も含む)の更新を、[再レンダー](/learn/render-and-commit#re-renders-when-state-updates)をあまりに頻繁にトリガすることなしに行うことができます。これは、イベントハンドラおよびその中のコードがすべて完了した*後*まで、UI は更新されないということでもあります。このような動作は**バッチ処理**(バッチング)とも呼ばれ、これにより React アプリの動作がずっと高速になります。またこれは、変数のうち一部のみが更新された「中途半端な」レンダー結果に混乱させられずに済むということでもあります。
-**React does not batch across *multiple* intentional events like clicks**--each click is handled separately. Rest assured that React only does batching when it's generally safe to do. This ensures that, for example, if the first button click disables a form, the second click would not submit it again.
+**React は、クリックのような意図的に引き起こされるイベントが*複数*ある場合、それらのバッチ処理を行いません**。各クリックは別々に処理されます。React は一般的に安全と判断される場合にのみバッチ処理を行いますので、安心してください。たとえば、最初のボタンクリックでフォームを無効にしたのであれば、2 度目のクリックでフォームが再び送信されてしまわないことが保証されます。
-## Updating the same state multiple times before the next render {/*updating-the-same-state-multiple-times-before-the-next-render*/}
+## 次のレンダー前に同じ state を複数回更新する {/*updating-the-same-state-multiple-times-before-the-next-render*/}
-It is an uncommon use case, but if you would like to update the same state variable multiple times before the next render, instead of passing the *next state value* like `setNumber(number + 1)`, you can pass a *function* that calculates the next state based on the previous one in the queue, like `setNumber(n => n + 1)`. It is a way to tell React to "do something with the state value" instead of just replacing it.
+一般的なユースケースではありませんが、次のレンダー前に同じ state 変数を複数回更新する場合、`setNumber(number + 1)` のようにして*次の state 値*を渡す代わりに、`setNumber(n => n + 1)` のようにキュー内のひとつ前の state に基づいて次の state を計算する*関数*を渡すことができます。これは、state の値を単に置き換える代わりに、React に「その state の値に対してこのようにせよ」と伝えるための手段です。
-Try incrementing the counter now:
+このカウンタをインクリメントしてみてください。
@@ -99,10 +99,10 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
-Here, `n => n + 1` is called an **updater function.** When you pass it to a state setter:
+ここで、`n => n + 1` は**更新用関数 (updater function)** と呼ばれます。これを state のセッタに渡すと:
-1. React queues this function to be processed after all the other code in the event handler has run.
-2. During the next render, React goes through the queue and gives you the final updated state.
+1. React はこの関数をキューに入れて、イベントハンドラ内の他のコードがすべて実行された後に処理されるようにします。
+2. 次のレンダー中に、React はキューを処理し、最後に更新された state を返します。
```js
setNumber(n => n + 1);
@@ -110,26 +110,26 @@ setNumber(n => n + 1);
setNumber(n => n + 1);
```
-Here's how React works through these lines of code while executing the event handler:
+以下に、イベントハンドラを実行するときに、React はこれらのコードをどのように処理するかを示します。
-1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
-1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
-1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
+1. `setNumber(n => n + 1)`: `n => n + 1` は関数。React はこれをキューに追加する。
+1. `setNumber(n => n + 1)`: `n => n + 1` は関数。React はこれをキューに追加する。
+1. `setNumber(n => n + 1)`: `n => n + 1` は関数。React はこれをキューに追加する。
-When you call `useState` during the next render, React goes through the queue. The previous `number` state was `0`, so that's what React passes to the first updater function as the `n` argument. Then React takes the return value of your previous updater function and passes it to the next updater as `n`, and so on:
+次のレンダー中に `useState` が呼び出されると、React はこのキューを処理します。前回 `number` という state の値は `0` だったので、それがひとつ目の更新用関数の引数 `n` に渡されます。React はひとつ前の更新用関数の返り値を取得し、それを次の更新用関数の `n` に渡し、というように続いていきます:
-| queued update | `n` | returns |
+| キュー内の更新処理 | `n` | 返り値 |
|--------------|---------|-----|
| `n => n + 1` | `0` | `0 + 1 = 1` |
| `n => n + 1` | `1` | `1 + 1 = 2` |
| `n => n + 1` | `2` | `2 + 1 = 3` |
-React stores `3` as the final result and returns it from `useState`.
+React は `3` を最終結果として保存し、`useState` から返します。
-This is why clicking "+3" in the above example correctly increments the value by 3.
-### What happens if you update state after replacing it {/*what-happens-if-you-update-state-after-replacing-it*/}
+以上が、上記の例で "+3" をクリックすると、値が正しく 3 ずつ増加する理由です。
+### state を置き換えた後に更新するとどうなるか {/*what-happens-if-you-update-state-after-replacing-it*/}
-What about this event handler? What do you think `number` will be in the next render?
+では、このイベントハンドラはどうでしょうか? 次回のレンダーで `number` の値はどうなっていると思いますか?
```js
{
@@ -165,29 +165,29 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
-Here's what this event handler tells React to do:
+このイベントハンドラでは、React に次のように指示しています。
-1. `setNumber(number + 5)`: `number` is `0`, so `setNumber(0 + 5)`. React adds *"replace with `5`"* to its queue.
-2. `setNumber(n => n + 1)`: `n => n + 1` is an updater function. React adds *that function* to its queue.
+1. `setNumber(number + 5)`: `number` は `0` なので、`setNumber(0 + 5)`。React はキューに *"`5` に置き換えよ"* という命令を追加する。
+2. `setNumber(n => n + 1)`: `n => n + 1` は更新用関数。React は*その関数*をキューに追加する。
-During the next render, React goes through the state queue:
+次のレンダー時、React は state 更新キューを処理します。
-| queued update | `n` | returns |
+| キュー内の更新処理 | `n` | 返り値 |
|--------------|---------|-----|
-| "replace with `5`" | `0` (unused) | `5` |
+| "`5` に置き換えよ" | `0` (未使用) | `5` |
| `n => n + 1` | `5` | `5 + 1 = 6` |
-React stores `6` as the final result and returns it from `useState`.
+React は `6` を最終結果として保存し、`useState` から返します。
-You may have noticed that `setState(5)` actually works like `setState(n => 5)`, but `n` is unused!
+お気づきかもしれませんが、`setState(5)` とは、実際には `n` の使用されない `setState(n => 5)` と同じように動作します!
-### What happens if you replace state after updating it {/*what-happens-if-you-replace-state-after-updating-it*/}
+### state を更新した後に置き換えるとどうなるか {/*what-happens-if-you-replace-state-after-updating-it*/}
-Let's try one more example. What do you think `number` will be in the next render?
+もうひとつ別の例を試してみましょう。次回のレンダーで `number` は何になると思いますか?
```js
{
@@ -225,32 +225,32 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
-Here's how React works through these lines of code while executing this event handler:
+このイベントハンドラを実行する際、React は以下の順番でコードを処理します。
-1. `setNumber(number + 5)`: `number` is `0`, so `setNumber(0 + 5)`. React adds *"replace with `5`"* to its queue.
-2. `setNumber(n => n + 1)`: `n => n + 1` is an updater function. React adds *that function* to its queue.
-3. `setNumber(42)`: React adds *"replace with `42`"* to its queue.
+1. `setNumber(number + 5)`: `number` は `0` なので `setNumber(0 + 5)`。React はキューに *"`5` に置き換えよ"* という命令を追加する。
+2. `setNumber(n => n + 1)`: `n => n + 1` は更新用関数。React は*その関数*をキューに追加する。
+3. `setNumber(42)`: React はキューに *"`42` に置き換えよ"* という命令を追加する。
-During the next render, React goes through the state queue:
+次のレンダー中に、React は state 更新キューを処理します。
-| queued update | `n` | returns |
+| キュー内の更新処理 | `n` | 返り値 |
|--------------|---------|-----|
-| "replace with `5`" | `0` (unused) | `5` |
+| "`5` に置き換えよ" | `0` (未使用) | `5` |
| `n => n + 1` | `5` | `5 + 1 = 6` |
-| "replace with `42`" | `6` (unused) | `42` |
+| "`42` に置き換えよ" | `6` (未使用) | `42` |
-Then React stores `42` as the final result and returns it from `useState`.
+というわけで、React は最終結果として `42` を保存し、`useState` から返します。
-To summarize, here's how you can think of what you're passing to the `setNumber` state setter:
+まとめると、`setNumber` という state セッタに渡すものを、以下のように考えることができます。
-* **An updater function** (e.g. `n => n + 1`) gets added to the queue.
-* **Any other value** (e.g. number `5`) adds "replace with `5`" to the queue, ignoring what's already queued.
+* **更新用関数**(例:`n => n + 1`)の場合、それがキューに追加されます。
+* **それ以外の値**(例:数値 `5`)の場合、ここまでのキューの内容を無視する "`5` に置き換えよ" のような命令を追加します。
-After the event handler completes, React will trigger a re-render. During the re-render, React will process the queue. Updater functions run during rendering, so **updater functions must be [pure](/learn/keeping-components-pure)** and only *return* the result. Don't try to set state from inside of them or run other side effects. In Strict Mode, React will run each updater function twice (but discard the second result) to help you find mistakes.
+イベントハンドラが完了した後、React は再レンダーをトリガします。再レンダー中に React はキューを処理します。アップデート関数はレンダー中に実行されるため、**更新用関数は[純関数](/learn/keeping-components-pure)である必要があり**、結果だけを*返す*ようにする必要があります。その中で state をセットしたり、他の副作用を実行したりしないでください。Strict Mode では、React は各更新用関数を 2 回実行します(ただし 2 つ目の結果は破棄されます)が、これによって間違いを見つけやすくなります。
-### Naming conventions {/*naming-conventions*/}
+### 命名規則 {/*naming-conventions*/}
-It's common to name the updater function argument by the first letters of the corresponding state variable:
+対応する state 変数の頭文字を使って更新用関数の引数の名前を付けることが一般的です。
```js
setEnabled(e => !e);
@@ -258,13 +258,13 @@ setLastName(ln => ln.reverse());
setFriendCount(fc => fc * 2);
```
-If you prefer more verbose code, another common convention is to repeat the full state variable name, like `setEnabled(enabled => !enabled)`, or to use a prefix like `setEnabled(prevEnabled => !prevEnabled)`.
+もっと長いコードが好きな場合、別の一般的な慣習としては、`setEnabled(enabled => !enabled)` のように完全な state 変数名を繰り返すか、`setEnabled(prevEnabled => !prevEnabled)` のようなプレフィクスを使用することがあります。
-* Setting state does not change the variable in the existing render, but it requests a new render.
-* React processes state updates after event handlers have finished running. This is called batching.
-* To update some state multiple times in one event, you can use `setNumber(n => n + 1)` updater function.
+* state をセットしても既存のレンダーの変数は変更されず、代わりに新しいレンダーが要求される。
+* React は、イベントハンドラが完了してから state の更新を処理する。これをバッチ処理と呼ぶ。
+* 1 つのイベントで複数回 state を更新したい場合 `setNumber(n => n + 1)` という形の更新用関数を使用できる。
@@ -272,13 +272,13 @@ If you prefer more verbose code, another common convention is to repeat the full
-#### Fix a request counter {/*fix-a-request-counter*/}
+#### リクエストカウンタの修正 {/*fix-a-request-counter*/}
-You're working on an art marketplace app that lets the user submit multiple orders for an art item at the same time. Each time the user presses the "Buy" button, the "Pending" counter should increase by one. After three seconds, the "Pending" counter should decrease, and the "Completed" counter should increase.
+あなたは、ユーザが美術品に対して複数の注文処理を同時並行で行える、アートマーケットアプリの開発をしています。ユーザが "Buy" ボタンを押すたびに、"Pending"(処理中)カウンタが 1 つずつ増えるようにする必要があります。3 秒後に "Pending" カウンタが 1 減り、"Completed" カウンタが 1 増える必要があります。
-However, the "Pending" counter does not behave as intended. When you press "Buy", it decreases to `-1` (which should not be possible!). And if you click fast twice, both counters seem to behave unpredictably.
+しかし、"Pending" カウンタは意図した通りに動作していません。"Buy" を押すと、"Pending" が `-1` に減少します(あり得ない!)。また、2 回素早くクリックすると、両方のカウンタが予測不可能な挙動を示します。
-Why does this happen? Fix both counters.
+なぜこれが起こるのでしょうか? 両方のカウンタを修正してください。
@@ -322,7 +322,7 @@ function delay(ms) {
-Inside the `handleClick` event handler, the values of `pending` and `completed` correspond to what they were at the time of the click event. For the first render, `pending` was `0`, so `setPending(pending - 1)` becomes `setPending(-1)`, which is wrong. Since you want to *increment* or *decrement* the counters, rather than set them to a concrete value determined during the click, you can instead pass the updater functions:
+`handleClick` イベントハンドラ内では、`pending` と `completed` の値はクリックイベントが起きた時点での値に対応しています。最初のレンダーでは、`pending` は `0` だったため、`setPending(pending - 1)` は `setPending(-1)` となり、これは間違いです。カウンタを*インクリメント*または*デクリメント*したいので、クリック時に決まる具体的な値をセットするのではなく、代わりに更新用関数を渡すことができます。
@@ -364,23 +364,23 @@ function delay(ms) {
-This ensures that when you increment or decrement a counter, you do it in relation to its *latest* state rather than what the state was at the time of the click.
+これにより、カウンタをインクリメントまたはデクリメントする際に、クリック時の state ではなく、最新の state に対して行われることが保証されます。
-#### Implement the state queue yourself {/*implement-the-state-queue-yourself*/}
+#### state キューの独自実装 {/*implement-the-state-queue-yourself*/}
-In this challenge, you will reimplement a tiny part of React from scratch! It's not as hard as it sounds.
+このチャレンジ問題では、React のごく一部をゼロから再実装します! それほど難しくありません。
-Scroll through the sandbox preview. Notice that it shows **four test cases.** They correspond to the examples you've seen earlier on this page. Your task is to implement the `getFinalState` function so that it returns the correct result for each of those cases. If you implement it correctly, all four tests should pass.
+サンドボックスプレビューをスクロールしてください。**4 つのテストケース**が表示されていることに注意してください。それらはこのページの先ほどの例に対応しています。あなたの仕事は、`getFinalState` 関数を実装して、それぞれのケースに対して正しい結果を返すことです。正しく実装すると、すべてのテストが通るはずです。
-You will receive two arguments: `baseState` is the initial state (like `0`), and the `queue` is an array which contains a mix of numbers (like `5`) and updater functions (like `n => n + 1`) in the order they were added.
+2 つの引数を受け取ることになります。`baseState` は初期 state (例えば `0`)であり、`queue` は数値(例えば `5`)または更新用関数(例えば `n => n + 1`)のいずれかが、キューに入れられた順番で入っている配列です。
-Your task is to return the final state, just like the tables on this page show!
+あなたの仕事は、このページ内の表で見たような処理を行って、最終的な state を返すことです!
-If you're feeling stuck, start with this code structure:
+難しいと感じたら、次のコード構造から始めてください:
```js
export function getFinalState(baseState, queue) {
@@ -398,7 +398,7 @@ export function getFinalState(baseState, queue) {
}
```
-Fill out the missing lines!
+足りない行を埋めてください!
@@ -495,7 +495,7 @@ function TestCase({
-This is the exact algorithm described on this page that React uses to calculate the final state:
+以下が、このページで説明してきたような形で React が最終 state を計算するために使用しているアルゴリズムそのものです:
@@ -596,7 +596,7 @@ function TestCase({
-Now you know how this part of React works!
+これで、この部分で React がどのように動作するのかが分かりましたね!
diff --git a/src/content/learn/state-as-a-snapshot.md b/src/content/learn/state-as-a-snapshot.md
index 503b0abb4..3a43214be 100644
--- a/src/content/learn/state-as-a-snapshot.md
+++ b/src/content/learn/state-as-a-snapshot.md
@@ -1,27 +1,27 @@
---
-title: State as a Snapshot
+title: state はスナップショットである
---
-State variables might look like regular JavaScript variables that you can read and write to. However, state behaves more like a snapshot. Setting it does not change the state variable you already have, but instead triggers a re-render.
+state 変数は、読んだり書いたりできる普通の JavaScript の変数のように見えるかもしれません。しかし、state はむしろ、スナップショットのように振る舞います。state をセットしても、既にある state 変数は変更されず、かわりに再レンダーがトリガされます。
-* How setting state triggers re-renders
-* When and how state updates
-* Why state does not update immediately after you set it
-* How event handlers access a "snapshot" of the state
+* state のセットが再レンダーをどのようにトリガするのか
+* state がいつどのように更新されるか
+* state がセットされた直後に更新されない理由
+* イベントハンドラが state の「スナップショット」にどのようにアクセスするのか
-## Setting state triggers renders {/*setting-state-triggers-renders*/}
+## state のセットでレンダーがトリガされる {/*setting-state-triggers-renders*/}
-You might think of your user interface as changing directly in response to the user event like a click. In React, it works a little differently from this mental model. On the previous page, you saw that [setting state requests a re-render](/learn/render-and-commit#step-1-trigger-a-render) from React. This means that for an interface to react to the event, you need to *update the state*.
+ユーザインターフェースとはクリックなどのユーザイベントに直接反応して更新されるものだ、と考えているかもしれません。React の動作は、このような考え方とは少し異なります。前のページで、[state をセットすることで再レンダーを React に要求](/learn/render-and-commit#step-1-trigger-a-render)しているのだ、ということを見てきました。これは、インターフェースがイベントに応答するためには、*state を更新*する必要があることを意味します。
-In this example, when you press "send", `setIsSent(true)` tells React to re-render the UI:
+この例では、"Send" を押すと、`setIsSent(true)` が React に UI の再レンダーを指示します。
@@ -61,43 +61,43 @@ label, textarea { margin-bottom: 10px; display: block; }
-Here's what happens when you click the button:
+ボタンをクリックすると次のような処理が行われます:
-1. The `onSubmit` event handler executes.
-2. `setIsSent(true)` sets `isSent` to `true` and queues a new render.
-3. React re-renders the component according to the new `isSent` value.
+1. `onSubmit` イベントハンドラが実行されます。
+2. `setIsSent(true)` が `isSent` を `true` にセットし、新しいレンダーを予約します。
+3. React が新しい `isSent` の値を使ってコンポーネントを再レンダーします。
-Let's take a closer look at the relationship between state and rendering.
+state とレンダーの関係をもう少し詳しく見ていきましょう。
-## Rendering takes a snapshot in time {/*rendering-takes-a-snapshot-in-time*/}
+## レンダーは時間を切り取ってスナップショットを取る {/*rendering-takes-a-snapshot-in-time*/}
-["Rendering"](/learn/render-and-commit#step-2-react-renders-your-components) means that React is calling your component, which is a function. The JSX you return from that function is like a snapshot of the UI in time. Its props, event handlers, and local variables were all calculated **using its state at the time of the render.**
+[「レンダーする」](/learn/render-and-commit#step-2-react-renders-your-components)とは、React があなたのコンポーネント(関数)を呼び出すということです。関数から返される JSX は、その時点での UI のスナップショットのようなものです。その JSX 内の props、イベントハンドラ、ローカル変数はすべて、**レンダー時の state を使用して計算されます**。
-Unlike a photograph or a movie frame, the UI "snapshot" you return is interactive. It includes logic like event handlers that specify what happens in response to inputs. React updates the screen to match this snapshot and connects the event handlers. As a result, pressing a button will trigger the click handler from your JSX.
+写真や映画のフレームとは違い、返される「UI の スナップショット」はインタラクティブです。イベントハンドラのような、入力に対する応答を指定するためのロジックが含まれています。React は画面をこのスナップショットに合わせて更新し、イベントハンドラを接続します。その結果として、ボタンを押すと JSX に書いたクリックハンドラがトリガされます。
-When React re-renders a component:
+React がコンポーネントを再レンダーする際には:
-1. React calls your function again.
-2. Your function returns a new JSX snapshot.
-3. React then updates the screen to match the snapshot you've returned.
+1. React が再度あなたの関数を呼び出します。
+2. 関数は新しい JSX のスナップショットを返します。
+3. React は返されたスナップショットに合わせて画面を更新します。
-
-
-
+
+
+
-As a component's memory, state is not like a regular variable that disappears after your function returns. State actually "lives" in React itself--as if on a shelf!--outside of your function. When React calls your component, it gives you a snapshot of the state for that particular render. Your component returns a snapshot of the UI with a fresh set of props and event handlers in its JSX, all calculated **using the state values from that render!**
+コンポーネントのメモリとしての state は、関数が終了したら消えてしまう通常の変数とは異なります。state は実際には React 自体の中で「生存」しています。まるで棚に保管しているかのように、関数の外部で存在し続けます。React がコンポーネントを呼び出すとき、React はその特定のレンダーに対する state のスナップショットを提供します。あなたのコンポーネントは、props やイベントハンドラの新たな一式を揃えた JSX という形で UI のスナップショットを返し、それらはすべて**その特定のレンダー時の state の値を使って計算されます!**
-
-
-
+
+
+
-Here's a little experiment to show you how this works. In this example, you might expect that clicking the "+3" button would increment the counter three times because it calls `setNumber(number + 1)` three times.
+これがどのように動作するかを示す小さな実験をしましょう。この例では、"+3" ボタンをクリックすると `setNumber(number + 1)` を 3 回呼び出すので、カウンタが 3 回インクリメントされると予想するかもしれません。
-See what happens when you click the "+3" button:
+"+3" ボタンをクリックすると何が起こるか見てみましょう。
@@ -127,9 +127,9 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
-Notice that `number` only increments once per click!
+`number` がクリックごとに 1 しか増えていませんね!
-**Setting state only changes it for the *next* render.** During the first render, `number` was `0`. This is why, in *that render's* `onClick` handler, the value of `number` is still `0` even after `setNumber(number + 1)` was called:
+**state をセットしても、それが本当に変更されるのは*次回の*レンダーです**。最初のレンダーでは `number` が `0` でした。だから、*そのレンダーの* `onClick` ハンドラにおいては、`setNumber(number + 1)` が呼ばれた後も `number` が `0` のままだったのです。
```js
{
@@ -139,18 +139,18 @@ Notice that `number` only increments once per click!
}}>+3
```
-Here is what this button's click handler tells React to do:
+このボタンのクリックハンドラは、以下のように React に指示しています。
-1. `setNumber(number + 1)`: `number` is `0` so `setNumber(0 + 1)`.
- - React prepares to change `number` to `1` on the next render.
-2. `setNumber(number + 1)`: `number` is `0` so `setNumber(0 + 1)`.
- - React prepares to change `number` to `1` on the next render.
-3. `setNumber(number + 1)`: `number` is `0` so `setNumber(0 + 1)`.
- - React prepares to change `number` to `1` on the next render.
+1. `setNumber(number + 1)`: `number` が `0` なので `setNumber(0 + 1)`。
+ - React は次回のレンダーで `number` を `1` に更新する準備をする。
+2. `setNumber(number + 1)`: `number` が `0` なので `setNumber(0 + 1)`。
+ - React は次回のレンダーで `number` を `1` に更新する準備をする。
+3. `setNumber(number + 1)`: `number` が `0` なので `setNumber(0 + 1)`。
+ - React は次回のレンダーで `number` を `1` に更新する準備をする。
-Even though you called `setNumber(number + 1)` three times, in *this render's* event handler `number` is always `0`, so you set the state to `1` three times. This is why, after your event handler finishes, React re-renders the component with `number` equal to `1` rather than `3`.
+`setNumber(number + 1)` を 3 回呼び出しましたが、*今回のレンダーの*イベントハンドラでは `number` は常に `0` なので、state を 3 回連続して `1` にセットしていることになります。これが、イベントハンドラが終了した後、React が `number` を `3` ではなく `1` とした上でコンポーネントを再レンダーする理由です。
-You can also visualize this by mentally substituting state variables with their values in your code. Since the `number` state variable is `0` for *this render*, its event handler looks like this:
+もっと分かりやすくするために、頭の中でコード内の state 変数を実際の値に置換してみることもできます。*このレンダーでは* `number` という state 変数は `0` なので、イベントハンドラは次のようになっています。
```js
{
@@ -160,7 +160,7 @@ You can also visualize this by mentally substituting state variables with their
}}>+3
```
-For the next render, `number` is `1`, so *that render's* click handler looks like this:
+次のレンダーでは、`number` が `1` になるため、*そちらのレンダーの* クリックハンドラは、次のようになります。
```js
{
@@ -170,11 +170,11 @@ For the next render, `number` is `1`, so *that render's* click handler looks lik
}}>+3
```
-This is why clicking the button again will set the counter to `2`, then to `3` on the next click, and so on.
+以上が、ボタンを再度クリックするとカウンタが `2` にセットされ、次のクリックでは `3` にセットされ、というようになる理由です。
-## State over time {/*state-over-time*/}
+## 時間経過と state {/*state-over-time*/}
-Well, that was fun. Try to guess what clicking this button will alert:
+なかなか面白い話でした。それでは、このボタンをクリックするとアラートに何が表示されるか予想してみてください。
@@ -203,14 +203,14 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
-If you use the substitution method from before, you can guess that the alert shows "0":
+上記で説明した置換メソッドを使えば、アラートには "0" と表示されることがわかりますね。
```js
setNumber(0 + 5);
alert(0);
```
-But what if you put a timer on the alert, so it only fires _after_ the component re-rendered? Would it say "0" or "5"? Have a guess!
+でも、アラートにタイマーを設定して、コンポーネントが再レンダーされた*後に*発火するようにしたらどうなるでしょうか? "0" と表示されるのか、"5" と表示されるのか推測してみてください。
@@ -241,7 +241,7 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
-Surprised? If you use the substitution method, you can see the "snapshot" of the state passed to the alert.
+驚いたでしょうか? さきほどの置換メソッドを使ってみれば、アラートに渡された state が「スナップショット」であることが分かるでしょう。
```js
setNumber(0 + 5);
@@ -250,16 +250,16 @@ setTimeout(() => {
}, 3000);
```
-The state stored in React may have changed by the time the alert runs, but it was scheduled using a snapshot of the state at the time the user interacted with it!
+アラートが実行される時点では React に格納されている state は既に更新されているかもしれませんが、アラートはユーザがボタンを操作した時点での state のスナップショットを使ってスケジューリングされました!
-**A state variable's value never changes within a render,** even if its event handler's code is asynchronous. Inside *that render's* `onClick`, the value of `number` continues to be `0` even after `setNumber(number + 5)` was called. Its value was "fixed" when React "took the snapshot" of the UI by calling your component.
+イベントハンドラのコードが非同期であっても、**レンダー内の state 変数の値は決して変わりません**。*そのレンダーの* `onClick`内では、`setNumber(number + 5)` が呼ばれた後も `number` の値は `0` のままです。その値は React があなたのコンポーネントを呼び出して UI の「スナップショットを取った」時に、「固定」されたのです。
-Here is an example of how that makes your event handlers less prone to timing mistakes. Below is a form that sends a message with a five-second delay. Imagine this scenario:
+ここで、このお陰でタイミングにまつわる問題が起きづらくなっている、という例をお示しします。以下のフォームは、5 秒の遅延後にメッセージを送信します。ここでこんなシナリオを想像してみてください:
-1. You press the "Send" button, sending "Hello" to Alice.
-2. Before the five-second delay ends, you change the value of the "To" field to "Bob".
+1. "Send" ボタンを押して、"Hello" というメッセージをアリスに送る。
+2. 5 秒の遅延が終わる前に、"To" フィールドの値を "Bob" に変更する。
-What do you expect the `alert` to display? Would it display, "You said Hello to Alice"? Or would it display, "You said Hello to Bob"? Make a guess based on what you know, and then try it:
+`alert` に何が表示されると思いますか? "You said Hello to Alice" と表示されるのでしょうか? それとも "You said Hello to Bob" でしょうか? ここまでの知識に基づいて推測し、実際に試してみましょう。
@@ -305,19 +305,19 @@ label, textarea { margin-bottom: 10px; display: block; }
-**React keeps the state values "fixed" within one render's event handlers.** You don't need to worry whether the state has changed while the code is running.
+**React は、レンダー内の state の値を「固定」し、イベントハンドラ内で保持します**。コードが実行されている途中で state が変更されたかどうか心配する必要はありません。
-But what if you wanted to read the latest state before a re-render? You'll want to use a [state updater function](/learn/queueing-a-series-of-state-updates), covered on the next page!
+しかし、再レンダー前に最新の state を読み取りたい場合はどうでしょうか? [state 更新用関数](/learn/queueing-a-series-of-state-updates)を使うことができます。これについては次のページで説明します!
-* Setting state requests a new render.
-* React stores state outside of your component, as if on a shelf.
-* When you call `useState`, React gives you a snapshot of the state *for that render*.
-* Variables and event handlers don't "survive" re-renders. Every render has its own event handlers.
-* Every render (and functions inside it) will always "see" the snapshot of the state that React gave to *that* render.
-* You can mentally substitute state in event handlers, similarly to how you think about the rendered JSX.
-* Event handlers created in the past have the state values from the render in which they were created.
+- state のセットは新しいレンダーをリクエストする。
+- React は state をコンポーネントの外側で、まるで棚に保管しておくかのようにして保持する。
+- `useState` を呼び出すと、React は*そのレンダーのための* state のスナップショットを返す。
+- 変数やイベントハンドラは複数レンダーをまたいで「生き残る」ことはない。すべてのレンダーは固有のイベントハンドラを持つ。
+- 各レンダー(およびその中の関数)からは、常に、React が *その*レンダーに渡した state のスナップショットが「見える」。
+- レンダーされた JSX を考える時と同様にして、イベントハンドラ内の state を頭の中で実際の値に置換してみることができる。
+- 過去に作成されたイベントハンドラは、それが作成されたレンダーにおける state の値を持っている。
@@ -325,9 +325,9 @@ But what if you wanted to read the latest state before a re-render? You'll want
-#### Implement a traffic light {/*implement-a-traffic-light*/}
+#### 信号機を実装 {/*implement-a-traffic-light*/}
-Here is a crosswalk light component that toggles when the button is pressed:
+以下は、ボタンが押されると切り替わる歩行者用信号機のコンポーネントです。
@@ -362,13 +362,13 @@ h1 { margin-top: 20px; }
-Add an `alert` to the click handler. When the light is green and says "Walk", clicking the button should say "Stop is next". When the light is red and says "Stop", clicking the button should say "Walk is next".
+クリックハンドラに `alert` を追加してください。信号が緑で "Walk" と表示されている場合、ボタンをクリックすると "Stop is next" と表示され、信号が赤で "Stop" と表示されている場合、ボタンをクリックすると "Walk is next" と表示されるようにしてください。
-Does it make a difference whether you put the `alert` before or after the `setWalk` call?
+`alert` を `setWalk` の前に置いた場合と後に置いた場合で、違いはありますか?
-Your `alert` should look like this:
+`alert` は以下のように書けます。
@@ -404,17 +404,17 @@ h1 { margin-top: 20px; }
-Whether you put it before or after the `setWalk` call makes no difference. That render's value of `walk` is fixed. Calling `setWalk` will only change it for the *next* render, but will not affect the event handler from the previous render.
+`alert` を `setWalk` の前に置いた場合と後に置いた場合で、違いはありません。このレンダー中、`walk` の値は固定です。`setWalk` を呼び出しても、*次の*レンダーまで実際の変更は起きず、現在レンダーのイベントハンドラには影響しません。
-This line might seem counter-intuitive at first:
+この行は最初は直感に反しているように思えるかもしれません。
```js
alert(walk ? 'Stop is next' : 'Walk is next');
```
-But it makes sense if you read it as: "If the traffic light shows 'Walk now', the message should say 'Stop is next.'" The `walk` variable inside your event handler matches that render's value of `walk` and does not change.
+しかし、これを「もし信号が今 "Walk" を表示しているなら、メッセージは "Stop is next" と言うべきだ」のように読めば、理に適っていることが分かります。イベントハンドラ内の `walk` 変数は、そのレンダーの `walk` の値と一致しており、変わることはありません。
-You can verify that this is correct by applying the substitution method. When `walk` is `true`, you get:
+上記で説明している置換メソッドを適用して、このことが正しいことを確認することができます。`walk` が `true` の場合、次のようになります。
```js
{
@@ -428,7 +428,7 @@ You can verify that this is correct by applying the substitution method. When `w
```
-So clicking "Change to Stop" queues a render with `walk` set to `false`, and alerts "Stop is next".
+すなわち、"Change to Stop" をクリックすると、`walk` が `false` にセットされたレンダーがキューに入り、"Stop is next" というアラートが表示されます。
diff --git a/src/sidebarLearn.json b/src/sidebarLearn.json
index 13aef4415..32b889a71 100644
--- a/src/sidebarLearn.json
+++ b/src/sidebarLearn.json
@@ -103,11 +103,11 @@
"path": "/learn/render-and-commit"
},
{
- "title": "State as a Snapshot",
+ "title": "state はスナップショットである",
"path": "/learn/state-as-a-snapshot"
},
{
- "title": "Queueing a Series of State Updates",
+ "title": "一連の state の更新をキューに入れる",
"path": "/learn/queueing-a-series-of-state-updates"
},
{