Skip to content

Commit cfcfeff

Browse files
committed
Adding documentation for watch and proxied objects
1 parent 3ad6eaf commit cfcfeff

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/guide/reactivity-computed-watchers.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ watch([fooRef, barRef], ([foo, bar], [prevFoo, prevBar]) => {
205205
})
206206
```
207207

208+
### Watching Reactive Objects
209+
210+
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.
211+
212+
```js
213+
const numbers = reactive([1,2,3,4])
214+
215+
watch(
216+
() => [...numbers],
217+
(numbers, prevNumbers) =>{
218+
console.log(numbers, prevNumbers);
219+
})
220+
221+
numbers.push(5) // logs: [1,2,3,4,5] [1,2,3,4]
222+
```
223+
224+
For deeply nested object and arrays, a deep copy of values may be required. This can be achieved with a utility such as `lodash.cloneDeep`
225+
208226
### Shared Behavior with `watchEffect`
209227

210228
`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)