Skip to content

Commit 2a4e15a

Browse files
author
Johnny Charcosset
committed
docs: update watchEffects() part
1 parent 3ad513a commit 2a4e15a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/guide/essentials/watchers.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ watch(obj, (newValue, oldValue) => {
264264

265265
## `watchEffect()` \*\* {#watcheffect}
266266

267-
`watch()` is lazy: the callback won't be called until the watched source has changed. But in some cases we may want the same callback logic to be run eagerly - for example, we may want to fetch some initial data, and then re-fetch the data whenever relevant state changes. We may find ourselves doing this:
267+
`watch()` is lazy: the callback won't be called until the watched source has changed. But in some cases we may want the same callback logic to be run eagerly, an alternative to [`watch()`](/api/reactivity-core.html#watch) exists.
268+
269+
We may find ourselves doing this:
268270

269271
```js
270272
const url = ref('https://...')
@@ -278,7 +280,7 @@ async function fetchData() {
278280
// fetch immediately
279281
fetchData()
280282
// ...then watch for url change
281-
watch(url, fetchData)
283+
watch(url, fetchData, { immediate: true })
282284
```
283285

284286
This can be simplified with [`watchEffect()`](/api/reactivity-core.html#watcheffect). `watchEffect()` allows us to perform a side effect immediately while automatically tracking the effect's reactive dependencies. The above example can be rewritten as:

0 commit comments

Comments
 (0)