|
1 | 1 | ---
|
2 | 2 | id: faq-state
|
3 |
| -title: Component State |
| 3 | +title: コンポーネントの state |
4 | 4 | permalink: docs/faq-state.html
|
5 | 5 | layout: docs
|
6 | 6 | category: FAQ
|
7 | 7 | ---
|
8 | 8 |
|
9 |
| -### What does `setState` do? {#what-does-setstate-do} |
| 9 | +### `setState` は何をしているのですか? {#what-does-setstate-do} |
10 | 10 |
|
11 |
| -`setState()` schedules an update to a component's `state` object. When state changes, the component responds by re-rendering. |
| 11 | +`setState()` はコンポーネントの `state` オブジェクト更新をスケジュールします。state が更新されると、コンポーネントはそれに再レンダーで応じます。 |
12 | 12 |
|
13 |
| -### What is the difference between `state` and `props`? {#what-is-the-difference-between-state-and-props} |
| 13 | +### `state` と `props` の違いは何ですか? {#what-is-the-difference-between-state-and-props} |
14 | 14 |
|
15 |
| -[`props`](/docs/components-and-props.html) (short for "properties") and [`state`](/docs/state-and-lifecycle.html) are both plain JavaScript objects. While both hold information that influences the output of render, they are different in one important way: `props` get passed *to* the component (similar to function parameters) whereas `state` is managed *within* the component (similar to variables declared within a function). |
| 15 | +[`props`](/docs/components-and-props.html)("properties" を短くしたもの)と [`state`](/docs/state-and-lifecycle.html) は、両方ともプレーンな JavaScript のオブジェクトです。どちらもレンダー結果に影響を及ぼす情報を持ってはいますが、ある重要な一点が異なっています。つまり、`props` は(関数引数のように)コンポーネント*へ*渡されるのに対し、`state` は(関数内で宣言された変数のように)コンポーネント*の内部*で制御されます。 |
16 | 16 |
|
17 |
| -Here are some good resources for further reading on when to use `props` vs `state`: |
| 17 | +`props` と `state` のどちらをいつ使うべきかについて、こちらでより詳しく読むことができます。 |
18 | 18 | * [Props vs State](https://github.com/uberVU/react-guide/blob/master/props-vs-state.md)
|
19 | 19 | * [ReactJS: Props vs. State](http://lucybain.com/blog/2016/react-state-vs-pros/)
|
20 | 20 |
|
21 |
| -### Why is `setState` giving me the wrong value? {#why-is-setstate-giving-me-the-wrong-value} |
| 21 | +### `setState` が誤った値を返すのはなぜですか? {#why-is-setstate-giving-me-the-wrong-value} |
22 | 22 |
|
23 |
| -In React, both `this.props` and `this.state` represent the *rendered* values, i.e. what's currently on the screen. |
| 23 | +React では、`this.props` と `this.state` のいずれも、*レンダーされた*もの、つまりスクリーン上の値を表しています。 |
24 | 24 |
|
25 |
| -Calls to `setState` are asynchronous - don't rely on `this.state` to reflect the new value immediately after calling `setState`. Pass an updater function instead of an object if you need to compute values based on the current state (see below for details). |
| 25 | +`setState` 呼び出しは非同期です。呼び出し直後から `this.state` が新しい値を反映することを期待しないでください。もし現在の state に基づいた値を計算する必要がある場合は、オブジェクトの代わりに更新関数を渡してください。(詳しくは以下を参照) |
26 | 26 |
|
27 |
| -Example of code that will *not* behave as expected: |
| 27 | +このコード例は期待した通りには*動きません*。 |
28 | 28 |
|
29 | 29 | ```jsx
|
30 | 30 | incrementCount() {
|
31 |
| - // Note: this will *not* work as intended. |
| 31 | + // 補足:これは意図通りに*動きません* |
32 | 32 | this.setState({count: this.state.count + 1});
|
33 | 33 | }
|
34 | 34 |
|
35 | 35 | handleSomething() {
|
36 |
| - // Let's say `this.state.count` starts at 0. |
| 36 | + // `this.state.count` は 0 から始まるとします。 |
37 | 37 | this.incrementCount();
|
38 | 38 | this.incrementCount();
|
39 | 39 | this.incrementCount();
|
40 |
| - // When React re-renders the component, `this.state.count` will be 1, but you expected 3. |
| 40 | + // React がコンポーネントを再レンダーしても、`this.state.count` は意図通りの 3 ではなく 1 になります。 |
41 | 41 |
|
42 |
| - // This is because `incrementCount()` function above reads from `this.state.count`, |
43 |
| - // but React doesn't update `this.state.count` until the component is re-rendered. |
44 |
| - // So `incrementCount()` ends up reading `this.state.count` as 0 every time, and sets it to 1. |
| 42 | + // これは、上記の `incrementCount()` 関数は `this.state.count` の値を読むのですが、 |
| 43 | + // しかしコンポーネントが再レンダーされるまで React が `this.state.count` を更新しないためです。 |
| 44 | + // そして `incrementCount()` は値が 0 のままの `this.state.count` を毎回読み、そして 1 をセットしてしまいます。 |
45 | 45 |
|
46 |
| - // The fix is described below! |
| 46 | + // 対処法は下で説明していますよ! |
47 | 47 | }
|
48 | 48 | ```
|
49 | 49 |
|
50 |
| -See below for how to fix this problem. |
| 50 | +この問題を解決するには以下を見てください。 |
51 | 51 |
|
52 |
| -### How do I update state with values that depend on the current state? {#how-do-i-update-state-with-values-that-depend-on-the-current-state} |
| 52 | +### どうやって現在の state に依存する値を更新したらいいですか? {#how-do-i-update-state-with-values-that-depend-on-the-current-state} |
53 | 53 |
|
54 |
| -Pass a function instead of an object to `setState` to ensure the call always uses the most updated version of state (see below). |
| 54 | +`setState` へオブジェクトを渡す代わりに関数を渡してください。その関数は常に最新の状態の state を使って呼ばれることが保証されています。(次項参照) |
55 | 55 |
|
56 |
| -### What is the difference between passing an object or a function in `setState`? {#what-is-the-difference-between-passing-an-object-or-a-function-in-setstate} |
| 56 | +### `setState` へオブジェクトを渡すのと関数を渡すのとのでは何が違いますか? {#what-is-the-difference-between-passing-an-object-or-a-function-in-setstate} |
57 | 57 |
|
58 |
| -Passing an update function allows you to access the current state value inside the updater. Since `setState` calls are batched, this lets you chain updates and ensure they build on top of each other instead of conflicting: |
| 58 | +更新関数を渡すと、その関数内で現在の state の値へアクセスできるようになります。`setState` 呼び出しはバッチ処理されるため、更新処理を連結して、それぞれの更新が競合せずに順序だって動作することが保証されます。 |
59 | 59 |
|
60 | 60 | ```jsx
|
61 | 61 | incrementCount() {
|
62 | 62 | this.setState((state) => {
|
63 |
| - // Important: read `state` instead of `this.state` when updating. |
| 63 | + // 重要:更新には `this.state` ではなく `state` を使います。 |
64 | 64 | return {count: state.count + 1}
|
65 | 65 | });
|
66 | 66 | }
|
67 | 67 |
|
68 | 68 | handleSomething() {
|
69 |
| - // Let's say `this.state.count` starts at 0. |
| 69 | + // `this.state.count` は 0 から始まるとします。 |
70 | 70 | this.incrementCount();
|
71 | 71 | this.incrementCount();
|
72 | 72 | this.incrementCount();
|
73 | 73 |
|
74 |
| - // If you read `this.state.count` now, it would still be 0. |
75 |
| - // But when React re-renders the component, it will be 3. |
| 74 | + // ここで `this.state.count` を読んでもまだ 0 のままです。 |
| 75 | + // しかし React がコンポーネントを再レンダーするとき、値は 3 になります。 |
76 | 76 | }
|
77 | 77 | ```
|
78 | 78 |
|
79 |
| -[Learn more about setState](/docs/react-component.html#setstate) |
| 79 | +[setState についてもっと学ぶ](/docs/react-component.html#setstate) |
80 | 80 |
|
81 |
| -### When is `setState` asynchronous? {#when-is-setstate-asynchronous} |
| 81 | +### いつ `setState` は非同期になりますか? {#when-is-setstate-asynchronous} |
82 | 82 |
|
83 |
| -Currently, `setState` is asynchronous inside event handlers. |
| 83 | +現在、`setState` はイベントハンドラの内側では非同期です。 |
84 | 84 |
|
85 |
| -This ensures, for example, that if both `Parent` and `Child` call `setState` during a click event, `Child` isn't re-rendered twice. Instead, React "flushes" the state updates at the end of the browser event. This results in significant performance improvements in larger apps. |
| 85 | +例えばクリックイベントの間に `Parent` と `Child` の両方が `setState` を呼ぶとき、非同期処理のおかげで `Child` が 2 度レンダーされないことが保証されます。その代わりに React はブラウザイベントの最後に state の更新を「フラッシュ (flush)」します。これにより大規模アプリのパフォーマンスが大幅に向上します。 |
86 | 86 |
|
87 |
| -This is an implementation detail so avoid relying on it directly. In the future versions, React will batch updates by default in more cases. |
| 87 | +これは実装の詳細ですので、この仕組みに直接依存しないようにしてください。将来のバージョンにおいて、React はより多くの場合にバッチ更新するようになります。 |
88 | 88 |
|
89 |
| -### Why doesn't React update `this.state` synchronously? {#why-doesnt-react-update-thisstate-synchronously} |
| 89 | +### どうして React は `this.state` を同期的に更新しないのですか? {#why-doesnt-react-update-thisstate-synchronously} |
90 | 90 |
|
91 |
| -As explained in the previous section, React intentionally "waits" until all components call `setState()` in their event handlers before starting to re-render. This boosts performance by avoiding unnecessary re-renders. |
| 91 | +前項で説明したように、全てのコンポーネントがそのイベントハンドラ内で `setState()` を呼ぶまで、React は再レンダー前に意図的に「待つ」ようになっています。これにより不必要な再レンダーが防がれ、パフォーマンスが向上します。 |
92 | 92 |
|
93 |
| -However, you might still be wondering why React doesn't just update `this.state` immediately without re-rendering. |
| 93 | +とはいえ、React がどうして再レンダーなしに `this.state` を即時更新しないのか、まだ疑問に思っているかもしれません。 |
94 | 94 |
|
95 |
| -There are two main reasons: |
| 95 | +これには主に 2 つの理由があります。 |
96 | 96 |
|
97 |
| -* This would break the consistency between `props` and `state`, causing issues that are very hard to debug. |
98 |
| -* This would make some of the new features we're working on impossible to implement. |
| 97 | +* 同期的更新が `props` と `state` の間の一貫性を破壊し、非常にデバッグが難しい問題を引き起こしうるため。 |
| 98 | +* 同期的更新が、我々が取り組んでいる新機能のいくつかを実装不可能にしうるため。 |
99 | 99 |
|
100 |
| -This [GitHub comment](https://github.com/facebook/react/issues/11527#issuecomment-360199710) dives deep into the specific examples. |
| 100 | +この [GitHub コメント](https://github.com/facebook/react/issues/11527#issuecomment-360199710)は特定の例について詳しく解説しています。 |
101 | 101 |
|
102 |
| -### Should I use a state management library like Redux or MobX? {#should-i-use-a-state-management-library-like-redux-or-mobx} |
| 102 | +### Redux や MobX のような state 管理ライブラリを使うべきでしょうか? {#should-i-use-a-state-management-library-like-redux-or-mobx} |
103 | 103 |
|
104 |
| -[Maybe.](https://redux.js.org/faq/general#when-should-i-use-redux) |
| 104 | +[時には必要かもしれません。](https://redux.js.org/faq/general#when-should-i-use-redux) |
105 | 105 |
|
106 |
| -It's a good idea to get to know React first, before adding in additional libraries. You can build quite complex applications using only React. |
| 106 | +まずは他のライブラリを追加する前に React を理解することをお勧めします。React だけでも非常に複雑なアプリケーションを作り上げることができます。 |
0 commit comments