Skip to content

Commit f1a2192

Browse files
committed
1 parent 7cad06f commit f1a2192

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

src/guide/reactivity-computed-watchers.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ watchEffect(onInvalidate => {
7878
})
7979
})
8080
```
81+
8182
無効化するコールバックを直接返すのではなく、 `onInvalidate` 関数のコールバックを経由して登録しているのは、非同期のエラー処理では、返り値が非常に重要であるためです。データ取得を行う時、作用となる関数が非同期であることは非常に一般的なことです:
8283

8384
```js
@@ -95,6 +96,7 @@ watchEffect(async (onInvalidate) => {
9596
Vue のリアクティブシステムは、無効になった変更をバッファリングし、非同期に処理することによって、おなじ "tick" の中での複数の状態の変化に対して、不要な重複の呼び出しを避けることができています。内部的には、コンポーネントの `update` 関数も、監視されている作用の一つです。ユーザーによる作用がキューに入っている場合、デフォルトではすべてのコンポーネントの更新の **前に** 呼び出されます:
9697

9798
```html
99+
98100
<template>
99101
<div>{{ count }}</div>
100102
</template>
@@ -200,7 +202,7 @@ watch(count, (count, prevCount) => {
200202

201203
```js
202204
const firstName = ref('');
203-
const lastName= ref('');
205+
const lastName = ref('');
204206

205207
watch([firstName, lastName], (newValues, prevValues) => {
206208
console.log(newValues, prevValues);
@@ -210,6 +212,83 @@ firstName.value = "John"; // logs: ["John",""] ["", ""]
210212
lastName.value = "Smith"; // logs: ["John", "Smith"] ["John", ""]
211213
```
212214

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+
213292
### `watchEffect` との振る舞いの共有
214293

215294
`watch`[明示的な停止](#stopping-the-watcher)[副作用の無効化](#side-effect-invalidation) (代わりに第三引数に `onInvalidate` を渡すことになります)、[作用フラッシュのタイミング](#effect-flush-timing)ならびに[デバッグ手法](#watcher-debugging)についての挙動を[`watchEffect`](#watcheffect)と共有しています。

0 commit comments

Comments
 (0)