Effect
asd
watch()
Watches one or more reactive data sources and invokes a callback function when the sources change.
watch() is lazy by default - i.e. the callback is only called when the watched source has changed.
Parameters
First argument
- A getter function that returns a value
- A ref
- A reactive object
- ...or an array of the above.
Second argument
Callback that will be called when the source changes.
The callback receives three arguments: the new value, the old value, and a function for registering a side effect cleanup callback.
The cleanup callback will be called right before the next time the effect is re-run, and can be used to clean up invalidated side effects, e.g. a pending async request.
Third optional argument
immediate
trigger the callback immediately on watcher creation. Old value will be undefined on the first call.deep
force deep traversal of the source if it is an object, so that the callback fires on deep mutations.flush
adjust the callback's flush timingonTrack / onTrigger
debug the watcher's dependencies.
Example: Stopping the watcher
watchEffect()
Runs a function immediately while reactively tracking its dependencies and re-runs it whenever the dependencies are changed.
Parameters
First argument
The effect function receives a function that can be used to register a cleanup callback. The cleanup callback will be called right before the next time the effect is re-run, and can be used to clean up invalidated side effects, e.g. a pending async request.
Second optional argument
Object that can be used to adjust the effect's flush timing or to debug the effect's dependencies.
-
Setting flush: 'post' will defer the watcher until after component rendering.
-
In rare cases, it might be necessary to trigger a watcher immediately when a reactive dependency changes, e.g. to invalidate a cache. This can be achieved using flush: 'sync'. However, this setting should be used with caution, as it can lead to problems with performance and data consistency if multiple properties are being updated at the same time.
Example: simple
Example
watchPostEffect()
Alias of watchEffect() with flush: 'post' option.
watchSyncEffect()
Alias of watchEffect() with flush: 'sync' option.