You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/reactivity.md
+39-38
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,21 @@
1
-
# Reactivity in Depth
1
+
# リアクティブの探求
2
2
3
-
Now it’s time to take a deep dive! One of Vue’s most distinct features is the unobtrusive reactivity system. Models are proxied JavaScript objects. When you modify them, the view updates. It makes state management simple and intuitive, but it’s also important to understand how it works to avoid some common gotchas. In this section, we are going to dig into some of the lower-level details of Vue’s reactivity system.
<VideoLessonhref="https://www.vuemastery.com/courses/vue-3-reactivity/vue3-reactivity"title="Learn how how reactivity works in depth with Vue Mastery">Vue Mastery のリアクティブの探求に関する無料ビデオを視聴する</VideoLesson>
6
6
7
-
This term comes up in programming quite a bit these days, but what do people mean when they say it? Reactivity is a programming paradigm that allows us to adjust to changes in a declarative manner. The canonical example that people usually show, because it’s a great one, is an excel spreadsheet.
If you put the number two in the first cell, and the number 3 in the second and asked for the SUM, the spreadsheet would give it to you. No surprises there. But if you update that first number, the SUM automagically updates too.
16
+
最初のセルに数字の 2 を入力し、2 番目のセルに数字の 3 を入力して SUM を要求すると、スプレッドシートは SUM の結果を返してくれます。なんの驚きもありません。ただし、最初のセルの数字を更新すると、 SUM の結果もなんと自動的に更新されます。
15
17
16
-
JavaScript doesn’t usually work like this -- If we were to write something comparable in JavaScript:
If we update the first value, the sum is not adjusted.
34
+
最初の値を更新しても、合計値は調整されません。
33
35
34
-
So how would we do this in JavaScript?
36
+
では、 JavaScript を使って以下の要素をどうやって実現するのでしょうか。
35
37
36
-
-Detect when there’s a change in one of the values
37
-
-Track the function that changes it
38
-
-Trigger the function so it can update the final value
38
+
-いずれかの値に変化があった時に検出する
39
+
-それを変更する関数を追跡する
40
+
-最終的な値を更新できるように関数を発火させる
39
41
40
-
## How Vue Tracks These Changes
42
+
## Vue がこれらの変更を追跡する方法
41
43
42
-
When you pass a plain JavaScript object to an application or component instance as its `data`option, Vue will walk through all of its properties and convert them to [Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) using a handler with getters and setters. This is an ES6-only feature, but we offer a version of Vue 3 that uses the older `Object.defineProperty`to support IE browsers. Both have the same surface API, but the Proxy version is slimmer and offers improved performance.
<iframeheight="500"style="width: 100%;"scrolling="no"title="Proxies and Vue's Reactivity Explained Visually"src="https://codepen.io/sdras/embed/zYYzjBg?height=500&theme-id=light&default-tab=result"frameborder="no"allowtransparency="true"allowfullscreen="true">
@@ -48,9 +50,9 @@ When you pass a plain JavaScript object to an application or component instance
48
50
</iframe>
49
51
</div>
50
52
51
-
That was rather quick and requires some knowledge of [Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) to understand! So let’s dive in a bit. There’s a lot of literature on Proxies, but what you really need to know is that a **Proxy is an object that encases another object or function and allows you to intercept it.**
Ok, so far, we’re just wrapping that object and returning it. Cool, but not that useful yet. But watch this, we can also intercept this object while we wrap it in the Proxy. This interception is called a trap.
Beyond a console log, we could do anything here we wish. We could even _not_ return the real value if we wanted to. This is what makes Proxies so powerful for creating APIs.
95
+
コンソールログ以外にも、ここでは思い通りの操作が可能です。必要な場合は、実際の値を返さ _ない_ ようにすることさえできます。これにより、プロキシは API の作成において強力なものになっています。
94
96
95
-
Furthermore, there’s another feature Proxies offer us. Rather than just returning the value like this: `target[prop]`, we could take this a step further and use a feature called `Reflect`, which allows us to do proper `this` binding. It looks like this:
We mentioned before that in order to have an API that updates a final value when something changes, we’re going to have to set new values when something changes. We do this in the handler, in a function called `track`, where we pass in the `target`and`key`.
117
+
前述の通り、何らかの変更があった時に最終的な値を更新する API を実装するには、何らかの変更があった時に新しい値を設定する必要があるでしょう。この処理をハンドラー内の `track` という関数で、 `target`と`key` を引数として渡して行います。
116
118
117
119
```js{7}
118
120
const dinner = {
@@ -133,7 +135,7 @@ console.log(proxy.meal)
133
135
// tacos
134
136
```
135
137
136
-
Finally, we also set new values when something changes. For this, we’re going to set the changes on our new proxy, by triggering those changes:
The proxied object is invisible to the user, but under the hood they enable Vue to perform dependency-tracking and change-notification when properties are accessed or modified. As of Vue 3, our reactivity is now available in a [separate package](https://github.com/vuejs/vue-next/tree/master/packages/reactivity). One caveat is that browser consoles format differently when converted data objects are logged, so you may want to install [vue-devtools](https://github.com/vuejs/vue-devtools)for a more inspection-friendly interface.
The use of Proxy does introduce a new caveat to be aware with: the proxied object is not equal to the original object in terms of identity comparison (`===`). For example:
@@ -198,20 +200,19 @@ const wrapped = new Proxy(obj, handlers)
198
200
console.log(obj === wrapped) // false
199
201
```
200
202
201
-
The original and the wrapped version will behave the same in most cases, but be aware that they will fail
202
-
operations that rely on strong identity comparisons, such as `.filter()` or `.map()`. This caveat is unlikely to come up when using the options API, because all reactive state is accessed from `this` and guaranteed to already be proxies.
203
+
オリジナルとラップされたバージョンはほとんどの場合同じように動作しますが、 `.filter()` や `.map()` などの強力な同一性比較に依存する操作は失敗することに注意してください。オプション API を使用する場合、この注意点に出くわすことはほとんどありません。すべてのリアクティブな状態が `this` からアクセスされ、すでにプロキシだということが保証されているためです。
203
204
204
-
However, when using the composition API to explicitly create reactive objects, the best practice is to never hold a reference to the original raw object and only work with the reactive version:
205
+
しかし、コンポジション API を使用して明示的にリアクティブオブジェクトを作成する場合、元の生のオブジェクトへの参照を保持せず、次のようにリアクティブバージョンでのみ処理をすることがベストプラクティスです:
205
206
206
207
```js
207
208
constobj=reactive({
208
209
count:0
209
210
}) // no reference to original
210
211
```
211
212
212
-
## Watchers
213
+
## ウォッチャ
213
214
214
-
Every component instance has a corresponding watcher instance, which records any properties "touched" during the component’s render as dependencies. Later on when a dependency’s setter is triggered, it notifies the watcher, which in turn causes the component to re-render.
<iframeheight="500"style="width: 100%;"scrolling="no"title="Second Reactivity with Proxies in Vue 3 Explainer"src="https://codepen.io/sdras/embed/GRJZddR?height=500&theme-id=light&default-tab=result"frameborder="no"allowtransparency="true"allowfullscreen="true">
@@ -220,10 +221,10 @@ Every component instance has a corresponding watcher instance, which records any
220
221
</iframe>
221
222
</div>
222
223
223
-
When you pass an object to a component instance as data, Vue converts it to a proxy. This proxy enables Vue to perform dependency-tracking and change-notification when properties are accessed or modified. Each property is considered a dependency.
After the first render, a component would have tracked a list of dependencies — the properties it accessed during the render. Conversely, the component becomes a subscriber to each of these properties. When a proxy intercepts a set operation, the property will notify all of its subscribed components to re-render.
> If you are using Vue 2.x and below, you may be interested in some of the change detection caveats that exist for those versions, [explored in more detail here](change-detection.md).
0 commit comments