Skip to content

Commit 6ff094a

Browse files
docs: addng documentation for watch and proxied objects (#734)
* Adding documentation for watch and proxied objects * Update src/guide/reactivity-computed-watchers.md Co-authored-by: Natalia Tepluhina <[email protected]> * Added more documentation around using `deep` and deeply nested objects. * Whitespace * Whitespace * Update src/guide/reactivity-computed-watchers.md Co-authored-by: Natalia Tepluhina <[email protected]> * Update src/guide/reactivity-computed-watchers.md Co-authored-by: Natalia Tepluhina <[email protected]> * Update src/guide/reactivity-computed-watchers.md Co-authored-by: Natalia Tepluhina <[email protected]> * Simplify examples and update language Co-authored-by: Natalia Tepluhina <[email protected]>
1 parent ec0816e commit 6ff094a

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

src/guide/reactivity-computed-watchers.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ An async function implicitly returns a Promise, but the cleanup function needs t
9696
Vue's reactivity system buffers invalidated effects and flushes them asynchronously to avoid unnecessary duplicate invocation when there are many state mutations happening in the same "tick". Internally, a component's `update` function is also a watched effect. When a user effect is queued, it is by default invoked **before** all component `update` effects:
9797

9898
```html
99+
99100
<template>
100101
<div>{{ count }}</div>
101102
</template>
@@ -201,7 +202,7 @@ A watcher can also watch multiple sources at the same time using an array:
201202

202203
```js
203204
const firstName = ref('');
204-
const lastName= ref('');
205+
const lastName = ref('');
205206

206207
watch([firstName, lastName], (newValues, prevValues) => {
207208
console.log(newValues, prevValues);
@@ -211,6 +212,83 @@ firstName.value = "John"; // logs: ["John",""] ["", ""]
211212
lastName.value = "Smith"; // logs: ["John", "Smith"] ["John", ""]
212213
```
213214

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+
214292
### Shared Behavior with `watchEffect`
215293

216294
`watch` shares behavior with [`watchEffect`](#watcheffect) in terms of [manual stoppage](#stopping-the-watcher), [side effect invalidation](#side-effect-invalidation) (with `onInvalidate` passed to the callback as the 3rd argument instead), [flush timing](#effect-flush-timing) and [debugging](#watcher-debugging).

0 commit comments

Comments
 (0)