@@ -78,6 +78,7 @@ watchEffect(onInvalidate => {
78
78
})
79
79
})
80
80
```
81
+
81
82
無効化するコールバックを直接返すのではなく、 ` onInvalidate ` 関数のコールバックを経由して登録しているのは、非同期のエラー処理では、返り値が非常に重要であるためです。データ取得を行う時、作用となる関数が非同期であることは非常に一般的なことです:
82
83
83
84
``` js
@@ -95,6 +96,7 @@ watchEffect(async (onInvalidate) => {
95
96
Vue のリアクティブシステムは、無効になった変更をバッファリングし、非同期に処理することによって、おなじ "tick" の中での複数の状態の変化に対して、不要な重複の呼び出しを避けることができています。内部的には、コンポーネントの ` update ` 関数も、監視されている作用の一つです。ユーザーによる作用がキューに入っている場合、デフォルトではすべてのコンポーネントの更新の ** 前に** 呼び出されます:
96
97
97
98
``` html
99
+
98
100
<template >
99
101
<div >{{ count }}</div >
100
102
</template >
@@ -200,7 +202,7 @@ watch(count, (count, prevCount) => {
200
202
201
203
``` js
202
204
const firstName = ref (' ' );
203
- const lastName = ref (' ' );
205
+ const lastName = ref (' ' );
204
206
205
207
watch ([firstName, lastName], (newValues , prevValues ) => {
206
208
console .log (newValues, prevValues);
@@ -210,6 +212,83 @@ firstName.value = "John"; // logs: ["John",""] ["", ""]
210
212
lastName .value = " Smith" ; // logs: ["John", "Smith"] ["John", ""]
211
213
```
212
214
215
+ ### Watching Reactive Objects
216
+
217
+ Using a watcher to compare values of an array or object that are reactive requires that it has a copy made of just the values.
218
+
219
+ ``` js
220
+ const numbers = reactive ([1 , 2 , 3 , 4 ])
221
+
222
+ watch (
223
+ () => [... numbers],
224
+ (numbers , prevNumbers ) => {
225
+ console .log (numbers, prevNumbers);
226
+ })
227
+
228
+ numbers .push (5 ) // logs: [1,2,3,4,5] [1,2,3,4]
229
+ ```
230
+
231
+ Attempting to check for changes of properties in a deeply nested object or array will still require the ` deep ` option to be true:
232
+
233
+ ``` js
234
+ const state = reactive ({
235
+ id: 1 ,
236
+ attributes: {
237
+ name: " " ,
238
+ },
239
+ });
240
+
241
+ watch (
242
+ () => state,
243
+ (state , prevState ) => {
244
+ console .log (
245
+ " not deep " ,
246
+ state .attributes .name ,
247
+ prevState .attributes .name
248
+ );
249
+ }
250
+ );
251
+
252
+ watch (
253
+ () => state,
254
+ (state , prevState ) => {
255
+ console .log (
256
+ " deep " ,
257
+ state .attributes .name ,
258
+ prevState .attributes .name
259
+ );
260
+ },
261
+ { deep: true }
262
+ );
263
+
264
+ state .attributes .name = " Alex" ; // Logs: "deep " "Alex" "Alex"
265
+ ```
266
+
267
+ However, watching a reactive object or array will always return a reference to the current value of that object for both the current and previous value of the state. To fully watch deeply nested objects and arrays, a deep copy of values may be required. This can be achieved with a utility such as [ lodash.cloneDeep] ( https://lodash.com/docs/4.17.15#cloneDeep )
268
+
269
+ ``` js
270
+ import _ from ' lodash' ;
271
+
272
+ const state = reactive ({
273
+ id: 1 ,
274
+ attributes: {
275
+ name: " " ,
276
+ },
277
+ });
278
+
279
+ watch (
280
+ () => _ .cloneDeep (state),
281
+ (state , prevState ) => {
282
+ console .log (
283
+ state .attributes .name ,
284
+ prevState .attributes .name
285
+ );
286
+ }
287
+ );
288
+
289
+ state .attributes .name = " Alex" ; // Logs: "Alex" ""
290
+ ```
291
+
213
292
### ` watchEffect ` との振る舞いの共有
214
293
215
294
` watch ` は[ 明示的な停止] ( #stopping-the-watcher ) 、[ 副作用の無効化] ( #side-effect-invalidation ) (代わりに第三引数に ` onInvalidate ` を渡すことになります)、[ 作用フラッシュのタイミング] ( #effect-flush-timing ) ならびに[ デバッグ手法] ( #watcher-debugging ) についての挙動を[ ` watchEffect ` ] ( #watcheffect ) と共有しています。
0 commit comments