Skip to content

Commit 7b504b0

Browse files
NataliaTepluhinaLinusBorgskirtles-code
authored
Extended watching multiple sources explanation (#1156)
* feat: added example for watching multiple sources * feat: added nextTick example * fix: reverted formatting * Update src/guide/reactivity-computed-watchers.md Co-authored-by: Thorsten Lünborg <[email protected]> * Update src/guide/reactivity-computed-watchers.md Co-authored-by: skirtle <[email protected]> * Update src/guide/reactivity-computed-watchers.md Co-authored-by: skirtle <[email protected]> Co-authored-by: Thorsten Lünborg <[email protected]> Co-authored-by: skirtle <[email protected]>
1 parent 252bbfd commit 7b504b0

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

src/guide/reactivity-computed-watchers.md

+46-11
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ We are registering the invalidation callback via a passed-in function instead of
8383

8484
```js
8585
const data = ref(null)
86-
watchEffect(async (onInvalidate) => {
87-
onInvalidate(() => { /* ... */ }) // we register cleanup function before Promise resolves
86+
watchEffect(async onInvalidate => {
87+
onInvalidate(() => {
88+
/* ... */
89+
}) // we register cleanup function before Promise resolves
8890
data.value = await fetchData(props.id)
8991
})
9092
```
@@ -101,19 +103,19 @@ Vue's reactivity system buffers invalidated effects and flushes them asynchronou
101103
</template>
102104
103105
<script>
104-
export default {
105-
setup() {
106-
const count = ref(0)
106+
export default {
107+
setup() {
108+
const count = ref(0)
107109
108-
watchEffect(() => {
109-
console.log(count.value)
110-
})
110+
watchEffect(() => {
111+
console.log(count.value)
112+
})
111113
112-
return {
113-
count
114-
}
114+
return {
115+
count
115116
}
116117
}
118+
}
117119
</script>
118120
```
119121

@@ -211,6 +213,39 @@ firstName.value = 'John' // logs: ["John", ""] ["", ""]
211213
lastName.value = 'Smith' // logs: ["John", "Smith"] ["John", ""]
212214
```
213215

216+
However, if you are changing both watched sources simultaneously in the same method, the watcher will be executed only once:
217+
218+
```js{9-13}
219+
setup() {
220+
const firstName = ref('')
221+
const lastName = ref('')
222+
223+
watch([firstName, lastName], (newValues, prevValues) => {
224+
console.log(newValues, prevValues)
225+
})
226+
227+
const changeValues = () => {
228+
firstName.value = 'John'
229+
lastName.value = 'Smith'
230+
// logs: ["John", "Smith"] ["", ""]
231+
}
232+
233+
return { changeValues }
234+
}
235+
```
236+
237+
Note that multiple synchronous changes will only trigger the watcher once.
238+
239+
It is possible to force the watcher to trigger after every change by using the setting `flush: 'sync'`, though that isn't usually recommended. Alternatively, [nextTick](/api/global-api.html#nexttick) can be used to wait for the watcher to run before making further changes. e.g.:
240+
241+
```js
242+
const changeValues = async () => {
243+
firstName.value = 'John' // logs: ["John", ""] ["", ""]
244+
await nextTick()
245+
lastName.value = 'Smith' // logs: ["John", "Smith"] ["John", ""]
246+
}
247+
```
248+
214249
### Watching Reactive Objects
215250

216251
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.

0 commit comments

Comments
 (0)